Calling an instance of a static method

Good day!

I'm a little confused. I want to use a calendar, so I searched the Internet and came across the following code:

Ca1endar c = Calendar.getlnstance(); c.set(2011,2, 5,1,25); 

But I found out that this is a static method:

 Calendar.getlnstance(); 

Why can I get a calendar instance (abstract class) if the called method is static?

I really want to understand this. So next time, I can also create a Static method that can instantiate.

Thanks.

+6
java static
source share
5 answers

This is a static factory method. An idea is a method that calls the constructor, and it returns the constructed object. The body of Calendar.getInstance() could be something like this:

 return new SomeCalendar(now); 

where SomeCalender is a concrete implementation of the abstract Calendar class. Some of the advantages: you do not need to worry about the base class (as far as you know this is just a calendar), and the base implementation can change without affecting you (for example, the code can be changed to return new AnotherCalendar() and you do not need to change anything in your code )

Since this is a static method, you can call it the type itself ( Calendar.getInstance(); ), unlike an instance of this type ( Calendar c = ...; c.getInstance(); ).

+7
source share

The instance method requires that you already have an instance to call the method.

Static does not mean that you cannot accept instances as a parameter, return an instance as a result, or create an instance. It just means that the method can be called without the first instance, and the method does not have such a thing as this .

Normally, you would not want this factory method to be an instance method (non-static) because it meant that you would need to have an instance to create a new one. How would you create the first?

(Non-static factory methods exist, but they are more often used to create a different type of object. For example, the Builder or factory class usually has a method that instantiates some other class.)

+3
source share

Java does such things everywhere. Take a look at the XML API.

This is a way to maintain a unified interface on the Calendar system, even if the underlying implementation is changed. Yes, the method is a factory method, however, some of the rest of the answers seem to miss that it can return various Calendar implementations.

Thus, the calendar can be returned even if it is abstract. Too simplified implementation of getInstance() may be

 Calendar getInstance() { return new GregorianCalendar(); } 

The calling getInstance() does not need to know the type of implementation in order to get the current date and time.

I recommend viewing OpenJDK

+3
source share

The fact that the factory method is static does not conclude that the calendar is static - it simply provides an access mechanism for creating the Calendar.

Check out this post on how to use this template yourself: http://today.java.net/pub/a/today/2005/03/09/factory.html

It also explains a bit more about the principles underlying the template.

Secondly, I recommend reading Effective Java, although this is a great book, despite its age!

+2
source share

This is an alternative way to create a new object in java. Constructors are traditionally the way to go, but since there are some advantages to using a static factory, its preferred simple vanilla constructors are.

You really need to pick Effective Java from Joshua Bloch. His first paragraph in the book. Preference to a static factory for the constructor. The benefits he mentions include:

1_ Because the static factory has names that can be descriptive.

2_ Then, a return of any subtype can be made, and not just the type of a particular class.

3_ You can handle concepts such as caching and single player games using the factory method. (you do not need to blindly create a new object).

4_ To reduce the initialization code. For example, instead of List newList = new ArrayList, we may have a method such as getStringList ();

The fourth advantage is something that I donโ€™t really like, but I see that the Google Guava system has implemented it. Please take a book. His is one of the best books on programming for Java.

+1
source share

All Articles