File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Instantiating diff types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instantiating diff types" Watch "Instantiating diff types" New topic

Instantiating diff types

aakash bhatt
Ranch Hand

Joined: Jan 09, 2003
Posts: 182
What is the diff between these 3 invocation types
1) Cal a = new Cal();
2) Cal a = Cal.getInstance();
3)Class c = Class.forName("java.lang.String");
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Two and Three are essentially the same: You are using some sort of Factory to return the desired instance. In the latter case, you are specifying a parameter to the Factory indicating what instance you want. In the former case, youare just requesting the default instance. (This could be a Singleton instance or some other algorithm to define the a default instance, such as in the case of the java.util.Calendar class upon which this example seems to be based, an instance of the correct (locale-based) subclass of Calendar initialized to today's date).
The First example is merely an object creation; it does nothing special other than create an object and call the constructor.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

Two and Three are essentially the same:

Not quite.
In 2, a static factory method has been defined for Cal which returns an object which may or may not have been newly created.
In 3, a Class representing the Cal class is loaded and returned. No Cal instance is created.
[ July 15, 2003: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
1) Cal a = new Cal();

The most direct way to instantiate a class

2) Cal a = Cal.getInstance();

This method is specific to certain classes, GregorianCalendar being one of them. This won't work for all classes.

3) Class c = Class.forName("java.util.Date");

A highly dynamic way of instantiating a class by using reflection. Sometimes you have to add the newInstance() method on the end.

Date d = Class.forName("java.util.Date").newInstance();
(Thanks, Joel)
[ July 15, 2003: Message edited by: Marilyn de Queiroz ]

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Originally posted by Bear Bibeault:

Not quite.
[ July 15, 2003: Message edited by: Bear Bibeault ]

True, but I wan't referring to the results when I said they were essentailly the same, but rather to the process/architecture used to acheive those results.
And Example three results in the java.lang.String.class object, not the Cal.class object
I agree. Here's the link:
subject: Instantiating diff types
jQuery in Action, 3rd edition