Forum Stats

  • 3,826,761 Users
  • 2,260,705 Discussions


Different ways of creating objects?

edited Apr 30, 2018 9:05PM in New To Java

Hi All,

I am new to Java and am studying a text book on the subject.

I have come across the following two lines of code; each of which creates a new object -

Calendar cal = Calendar.getInstance();

Date now = new Date();

I have a reasonable understanding of the 'new' keyword, and it's relationship to creating objects.

However I am wondering if anybody can explain to me, or point me in the direction of a good resource, which thoroughly explains why some objects are created with dot(.) notation, e.g.: Calendar.getInstance();, and others are created with the 'new' keyword? I am anticipating that the difference will relate to 'System' classes, but I have no idea of what these actually are!

Any help here would be greatly appreciated.

Kind Regards,



  • Unknown
    edited Apr 30, 2018 12:16PM

    It's good that you found a book you can use to help you learn Java.

    But the Java API docs are the best place to get info about classes and methods that belong to Java.

    A simple web search for 'calendar.getInstance' returns the following link as the FIRST RESULT

    Then search that page for 'getInstance' and you will find this

    <strong><a href=""> getInstance</a></strong>(<a href="" title="class in java.util">Locale</a> aLocale)Gets a calendar using the default time zone and specified locale.static <a href="" title="class in java.util">Calendar</a>

    and that link takes you further down the page to this

    getInstancepublic static Calendar getInstance()

    Gets a calendar using the default time zone and locale. The Calendar returned is based on the current time in the default time zone with the default locale.

    a Calendar.

    That shows that 'getInstance' is a static method that returns an instance of the 'Calendar' class.

    Examine the API and you will see there are more methods named 'getInstance' that each take different parameters.

    Those methods are FACTORY methods. You use them as a 'factory' to create objects. The 'factory' you use determines the exact characteristics of the object that gets created.

    Just as a car 'factory' can create blue cars a different car factory might create red cars, or one might create an electric car and another a diesel.

    Different factories for different objects. The objects have a LOT in common but can have different characteristics.

    The NEW operator just creates a VANILLA object using all of the default characteristics.

This discussion has been closed.