aspose file tools*
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
Author

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
Bartender

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
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

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.
hth,
bear
[ July 15, 2003: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
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 ]

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

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: http://aspose.com/file-tools
 
subject: Instantiating diff types