Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 545 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 440 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Different ways of creating objects?

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
-
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.
-
link to stack overflow discussion:
https://stackoverflow.com/questions/3170159/difference-between-calling-new-and-getinstance