File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes difference between instantion and object. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between instantion and object." Watch "difference between instantion and object." New topic
Author

difference between instantion and object.

ramchander yshetti
Ranch Hand

Joined: Apr 22, 2006
Posts: 97
please tell me about the difference between instantion and object.

i.e Class.forName(........)
gives the instantion of class, how it different from object
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Reader,
An instance of a class is referred to as an object. The following piece of code:

also returns an object by creating an instance of the class. It is just another alternative available to create an object. I hope what I have written is correct because I am not completely sure.


With Best Regards,
Shyam Prasad Murarka
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'd bet this is what you meant by the dots but to be more particular Class.forName(MyClass) returns an instance of Class. Once you have that, you can create an instance of MyClass:

Class c = class.forName( MyClass );
MyClass myInstance = MyClass.newInstance();

This is in most ways equivalent to:

MyClass myInstance = new MyClass();


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
Slight correction... say you are talking about a class named com.acme.whizbang.SpiffyThing:



does not return a "SpiffyThing" object.... it returns a java.lang.Class object... in other words, an instance of the class "java.lang.Class".... that object represents the class com.acme.whizbang.SpiffyThing, in the same way that an Integer object can represent an int value, or a Double represents a double. The Class object has methods to get information about the SpiffyThing class. It also has the following method:



The Class method newInstance() is a way to instantiate (get an object of) a class when you don't know the name of the class until runtime (for example, maybe it came out of a database). A lot of times, this is the only reason you need the Class object, so those two lines are combined into one (and no reference to the Class object is kept):



That works out to be the same as:




Anyway, if you're at the point in your understanding where you're not clear about the distinction between classes and objects, just be aware that java.lang.Class exists, and that it can be a source of confusion.

class - blueprint for, or type of, object
object - a manifestation of the blueprint
instance - synonym for Object

Hope this helps
-- Jon
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
ok, timing of my last reply looked like I was addressing Stan's post, but I just type slowly ;-).

Now I am addressing Stan's post.

Stan wrote:



This should have been (bolded where it is changed):



-- Jon
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Readers,
Thanks very much! I now have a clear understanding of this topic.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Jon, thanks for the corrections. I got caught between thinking myClass was a variable and a string name.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between instantion and object.
 
Similar Threads
Preventing instantion of base class object
difference between PageContext and Page object
object Vs instance
difference between instantion and object
Related to Object