Forum Stats

  • 3,826,761 Users
  • 2,260,705 Discussions
  • 7,897,072 Comments

Discussions

Different ways of creating objects?

c4bef551-b678-4eed-950e-318f21a47a28
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,

Davo

Answers

  • 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

    https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html

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

    <strong><a href="https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getInstance(java.util.Locale)"> getInstance</a></strong>(<a href="https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html" title="class in java.util">Locale</a> aLocale)Gets a calendar using the default time zone and specified locale.static <a href="https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html" 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.

    Returns:
    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.