7 Replies - 6959 Views - Last Post: 27 February 2010 - 02:01 PM Rate Topic: -----

#1 Guest_evan*


Reputation:

New to Java Question - getInstance()

Posted 26 February 2010 - 09:21 AM

The calendar class that the Java API comes with has a method called getInstance() in which you create an instance (or object) derivative of the class Calendar. This is because Calendar is abstract and therefor you can't instantiate it.

My question is, why is Calendar abstract if you are going to be instantiating it anyway using calendar's getInstance() method, why is it not just a non-abstract class? What am I missing?

Thanks.

Is This A Good Question/Topic? 0

Replies To: New to Java Question - getInstance()

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: New to Java Question - getInstance()

Posted 26 February 2010 - 09:41 AM

Quote

This is because Calendar is abstract and therefor you can't instantiate it.


That's not really the reason that getInstance is used. That is just an indication of a factory pattern being used (google it)

getInstance will return the kind of Calendar that's appropriate for your Locale, although of course you can create Calendar objects, such as GregorianCalendar directly
Was This Post Helpful? 0
  • +
  • -

#3 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: New to Java Question - getInstance()

Posted 26 February 2010 - 09:41 AM

getInstance will pull what type of calendar it is (i.e. Gregorian , Japanese-Gregorian, etc...)

now if i wanted a Gregorian calendar i could create it 2 ways:
GregorianCalendar a = new GregorianCalendar();
//or
GregorianCalendar b = new Calendar();



The calendar is abstract, because its only there as a guideline for other calendars, and holds common data and methods. Another advantages is you can do this:
Calendar[] cals = new Calendar[ 3 ];
JapaneseGregorianCalendar c = new JapaneseGregorianCalendar();

cals[0] = a;
cals[1] = b;
cals[2] = c;

for( int i = 0; i < cals.length(); i++ ){
  if( cals[i] instanceOf GregorianCalendar ){
        System.out.println("Hello");
  }else{
        System.out.println("World");
  }
}

///or

if( b.getInstance() == a.getInstance() )
  System.out.println( "same class!");


Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: New to Java Question - getInstance()

Posted 26 February 2010 - 09:42 AM

Good question, however you have to look at what the getInstance method is returning. getInstance returns the Calendar using the default timezone and locale. This is most likely the GregorianCalender which is a class that IS a Calender. Just view the calandar class as a factory class, which can return many types of instances depending on the system it is being used by
Was This Post Helpful? 0
  • +
  • -

#5 Guest_evan*


Reputation:

Re: New to Java Question - getInstance()

Posted 26 February 2010 - 09:53 AM

Thanks for all the fast replies! Definitely cleared it up for me. (I'm so afraid to move on until I fully understand something.) :)
Was This Post Helpful? 0

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: New to Java Question - getInstance()

Posted 26 February 2010 - 09:23 PM

[quote name='evan' date='26 February 2010 - 10:21 AM' timestamp='1267201265' post='940022']
The calendar class that the Java API comes with has a method called getInstance() in which you create an instance (or object) derivative of the class Calendar. This is because Calendar is abstract and therefor you can't instantiate it.
[/code]
Not really true... neither are the multiple answers posted

There is nothing magic about getInstance() and it is not a reserved name but it became kind of a de facto "standard". getInstance() is used for singleton (class that we want only one instance) for performance or synchronization reason (like to get a database connection).

Here is an example

class DbConnection {
   static DbConnection instance = null;

   // constructor ... private so you cannot directly call it
   private DbConnection() {
      ....  connect to database
   }
   static DbConnection getInstance() {
      // at first call let's create the single DbConnection object that will exist
      if(instance == null)
         instance = new DbConnection();
      // return the common one
      return instance;
   }
}



Hope this helps... happy coding
Was This Post Helpful? 1
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: New to Java Question - getInstance()

Posted 27 February 2010 - 01:40 AM

Quote

Not really true... neither are the multiple answers posted

There is nothing magic about getInstance() and it is not a reserved name but it became kind of a de facto "standard". getInstance() is used for singleton


That's not always the case. It's certainly not the case for Calendar
Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: New to Java Question - getInstance()

Posted 27 February 2010 - 02:01 PM

View Postg00se, on 27 February 2010 - 02:40 AM, said:

Quote

Not really true... neither are the multiple answers posted

There is nothing magic about getInstance() and it is not a reserved name but it became kind of a de facto "standard". getInstance() is used for singleton


That's not always the case. It's certainly not the case for Calendar

You're right as you mentionned it will return a Calendar for your locale.
I guess it is the exception that confirms the rule :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1