Title: Calling the instance of a static method
Tags: java;static
Question: Good day!

I am a little bit confused. I want to use a calendar so I searched it in the internet and encountered the following code:

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

But I learned that this is a static method:

```Calendar.getlnstance();
```

How come I can get the Instance of the Calendar(Abstract Class) if the method called is static? 

I really want to understand it. So next time I can also create a Static method that can create an instance. 

Thank you.

Comment: Thats  a static builder method. Its a replacement for using contructor.

Here is the accepted answer: It's the static factory method. The idea is the method is the one calling the constructor and it returns the constructed object.
The body of ```Calendar.getInstance()``` is perhaps something like this:

```return new SomeCalendar(now);
```

where ```SomeCalender``` is an concrete implementation of the abstract class ```Calendar```. Some of the advantages are: you don't have to care about the underlying class (as far as you know it's just a Calendar), and the underlying implementation can change without affecting you (for example, the code can  be changed to ```return new AnotherCalendar()``` and you don't have to change anything in your code)

Since it is a static method, you can call it on the type itself (```Calendar.getInstance();```) as opposed to an instance of that type (```Calendar c = ...; c.getInstance();```).

Comment for this answer: Hum I am surprised this is marked as the answer seeing as Calendar is abstract and most certainly the body of the method is not new Calendar(). The reason the method exists is so that you the underlying system can swap out the raw implementation of Calendar while maintaining the same interface which is Calendar.

Here is another answer: The fact the factory method is static does not infer that the Calendar is static - it just provides an access mechanism to create a Calendar. 

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

It also explains a bit more about the principles behind the pattern. 

I second the recommendation to read Effective Java though - it's a great book despite it's age!


Here is another answer: An instance method requires that you already have an instance to call the method on.

Being static does not mean that you can't take instances as a parameter, return an instance as a result or create an instance. It just means that the method can be called without first having an instance, and that in the method there is no such thing as ```this```.

You generally wouldn't want this sort of factory method to be an instance method (non-static) because it would mean you'd need to already have an instance to create a new one. How would you create the first one?

(Non-static factory methods do exist, but they are more commonly used for creating a different type of object. For example, a Builder or Factory class will typically have a method that creates instances of some other class.)


Here is another answer: Its a alternative way to create a new object in java. Traditionally constructors are the way to go, but since there are some advantages for using a static factory, its preferred over plain vanilla constructors.

You  should really pick up Effective Java by Joshua Bloch. Its the first item in the book. Preferring static factory for contructor. The advantages he mentions are:

1_ Since static factory have names that could be descriptive.

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

3_ You can handle concepts like caching and singletons with in a factory method. (you do not have to blindly create new object).  

4_ To reduce the initialization code. For instance instead of List newList = new ArrayList, we can have a method like getStringList();

The fourth advantage is something I am not very particular about, but I see Google Guava framework has implemented it.Please pick up the book. Its one of the best programming book for java.

Comment for this answer: @newbie: How about you go buy the book.
Comment for this answer: @Shakedown I don't have money. :( So I'll download it instead!
Comment for this answer: Actually, Item #1 is *consider* static factor methods instead of constructors. But in any case, I don't think this really answers the OP's question which seems to be more of a "how does this work" rather than a "why did they do it this way" sort of question.

Here is another answer: Java does this sort of thing all over the place. Look at the XML API.

This is a way to maintain a single interface into a ```Calendar``` system even if the underlying implementation is changed. Yes the method is a factory method, however, the point some of the other answers seem to miss is that it can return different implementations of ```Calendar```. 

That is how Calendar can be returned even if it is abstract. A overly simplified implementation of ```getInstance()``` could be 

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

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

I recommend looking through OpenJDK


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 5, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2011:00:03/05/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><static>', 'Title': 'Calling the instance of a static method', 'ViewCount': 993, 'id': 5201895, 'user_AboutMe': '<p>I\'m a newbie, a novice, an amateur, a beginner in programming. My Total Programming Experience is approximately equal to the no. of months I joined here! So pardon me for my idiotic questions. My dream is to be a great programmer someday. To make the impossible, possible. I want to be the <strong>female version of <a href="http://stackoverflow.com/users/22656/jon-skeet">Mr. Jon Skeet</a></strong> and my idol (who always scolds me hehehe) <strong><a href="http://stackoverflow.com/users/157882/balusc">Mr Balusc</strong></a>. But I think, based on my current capabilities, i need a whooping 50 years to be on their level.\nSo help me God!</p>\n\n<p>BTW, i really find this site super cool! I could interact and learn from many great programmers around the world. And for a programmer-wannabe like me, every opinion matters. What\'s important is that I\'m learning a lot here which I could not learn by just reading books. So thank you everyone for all the help. I think I am becoming a stackoverflow addict. </p>\n', 'user_AccountId': 249712.0, 'user_DisplayName': 'newbie', 'user_Reputation': 14286.0, 'user_Views': 2076.0, 'user_id': 525965.0}}