• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between instantion and object.

 
ramchander yshetti
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Jon Egan
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
Thanks very much! I now have a clear understanding of this topic.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic