Page 1 of 3 123 LastLast
Results 1 to 20 of 51
  1. #1
    JavaJunkie is offline Member
    Join Date
    Apr 2009
    Posts
    12
    Rep Power
    0

    Default difference between instance and object

    Hi,

    I really confused in the actual difference between instance and object. I searched a lot of over the difference between instance and object, but I couldn't find any relevant answer.

    For example, if I declare any class like:

    ClassA a=new ClassA();

    Is variable 'a' in above is an instance which has an object of ClassA type? Am I right?

    Thanks in advance.

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Ouch, never really thought of this before, but here goes.

    An object is the class itself. An instance of that class (created using new MyClass()) is like an Identical copy of the object, but you can call non-static methods on it, whereas you can only call static methods on the class.

    Not sure if all this is right, but I'm sure I'm close. You may not have found an answer because if is a somewhat hard topic...

    Hopefully this helps,
    Singing Boyo
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Think of the class like the blueprints of a building. When you go to make a building you use the blueprints to make it. When you make an instance of an object the class is used as the blueprints for creating the instance of the object.

    Java Code:
    ClassA a;
    ClassA -> The blueprints to follow when creating the object
    a -> the variable which will reference to the actual object

    if you were to do
    Java Code:
    ClassA a, b;
    b = a;
    Then b would point (reference) to the same instance as a. The variable is just used to reference to the object.

    Mr. Beans

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    From what I've been taught: an instance is an object.

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by Fubarable View Post
    From what I've been taught: an instance is an object.
    Hmm... But what about static methods/variables. If I have a class that extends JFrame(or acts as a component), and have static variables/methods, and then display it, I never created an instance. It is difficult, but possible.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Singing Boyo View Post
    If I have a class that extends JFrame(or acts as a component), and have static variables/methods, and then display it, I never created an instance.
    Could you show me what you mean? A static variable is static, but the object it refers to is, well simply an object or instance.

  7. #7
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Quote Originally Posted by Singing Boyo View Post
    An object is the class itself. An instance of that class (created using new MyClass()) is like an Identical copy of the object, but you can call non-static methods on it, whereas you can only call static methods on the class.
    Completely wrong I'm afraid. For all intents and purposes "instance" and "object" are completely interchangeable, "instance" usually being more descriptive.

    A class is a description of a class of objects, containing ALL code and static data. An object is an instance of something of this class, containing data pertaining to the individual instance.

    Java Code:
    ClassA a=new ClassA();
    "a" is a variable, containing (a reference to) an object/instance of type ClassA.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  8. #8
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Hmm... I may just be tired, but...

    I'm thinking of something with editable(non-final) static variables. In most of the large programs I create, I have numerous static objects that I modify with other (non-static) objects. I do create an instance of the class with a constructor to avoid creating another file, but if I really wanted to, I could perform all the constructor code in a different file. As well, the instance is never referred to in any other code. So what is the class if not an object?

    The declarations would be like this:
    Java Code:
    	public static ArraysManager arrmanage=new ArraysManager();
    	public static Inventory currentInventory;
    	public static MainUI ui;
    so... My class contains static variables editable by other classes. It could also contain methods to edit these variables. An instance is a collection of non-static variables, so a class that contains a collection of static variables is... almost the same. But not. Therefore my opinion.
    Last edited by Singing Boyo; 05-26-2009 at 02:21 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's not the "Class" that matters here, it's the objects. Your arrmanage for instance is a static variable, but it references an object or instance (notice that it has to be created with a call to "new" -- either that or a factory or some other object-creation mechanism), same for currentInventory, same for ui, etc... It matters not that you use the class name, a period, and then the variable name to reference the object, since again, these are just variables. The active entity though is and always will be an object.
    Last edited by Fubarable; 05-26-2009 at 03:20 AM.

  10. #10
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I'm referring to the StartUp class itself... I never create an instance of it that is used for anything but the code in it's constructor, and the code for the constructor could be moved to a static method.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  11. #11
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    For "could" probably read "should".
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  12. #12
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Instance and object are two terms for the same concept. An object is an instance of a class. I usually use instance because it seems a bit more specific.

    As far as static methods and variables, remember that those belong to an instance of the Class class. When a class is loaded, a single instance of Class is instantiated to represent it. Giving the Class class the name Sun did is confusing.

  13. #13
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    An instance of Class is indeed created and used to represent the class, but it does not contain any of the methods or static variables. These are stored in the static data area, while the Class object is allocated on the heap. Any objects created using the reflection API are separate from the concepts they encapsulate.

    A typical memory layout for a Java app, with the stack growing upwards into the free space, might be:

    --------
    Code (bytecode for class constructors and other methods/initialisers)
    --------
    Static data (anything declared static, plus string literals)
    --------
    Heap (for objects, incl. instances of Class, Method, Integer, etc.)
    --------
    Free space
    --------
    Stack (locally declared primitive variables, arrays and object pointers)
    --------
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  14. #14
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by OrangeDog View Post
    For "could" probably read "should".
    Hmm maybe... only take me 30 seconds. Even more of a "static class" (I just made that up... a class that is only used for it's static variables/methods related to the static variables)

    @Steve Forgot about Class objects. However, (and I'm on shaky ground here... never actually tried it) I believe that editing a static variable of a class with an instance of a class would edit it for all instances of that class. (I believe this even more after seeing OrangeDog's example.) Therefore, static variables are NOT stored in (a reference to) an instance. So... the question here is this: is the static variable stored as contained by some object, or not? If it is, than class files would be objects, and instances would be created using new MyClass(). If not, than instances and objects would be very much the same thing.

    I could be wrong, but a forum is for discussion and learning is it not?

    Singing Boyo
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  15. #15
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Hi,
    Instance means just pointing to memory location of an object whereas object is a exact thing which occupies the memory location.Just a guess ;)
    Ramya:cool:

  16. #16
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Hmm... Three sided debate... But an instance being a "pointer"? An instance is definitely an object (Just by definition of "instance") An instance is what you create using new MyClass(). The real question is what is an object.

    Great opinion Ramya, but I just don't see it as correct, though again, I could be wrong.

    And the debate goes on...

    Singing Boyo
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  17. #17
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Hi,
    k......My conclusion is both are same.
    Ramya:cool:

  18. #18
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    @OD- I'm not quite sure what you are saying. The JVM maintains the stack and the heap, not the app. In fact, there really are no "applications" in Java.

    AFAIK, all primitives and references go on the stack, objects' data goes on the heap. The byte code for all methods, static or otherwise, is loaded once.

    As far as statics, I don't believe the data is stored any differently than anything else. A static variable and an instance variable can both point at the same object on the heap...

    My point about static methods and variables had to do with how they are accessed. I was responding to this comment.

    Hmm... But what about static methods/variables. If I have a class that extends JFrame(or acts as a component), and have static variables/methods, and then display it, I never created an instance. It is difficult, but possible.
    Static methods and variables are accessed through an instance of Class. You obtain a reference to that instance by specifying the class' name. ClassLoader creates that instance when the class is first referenced.

    Apart from primitives and reference variables, everything I'm aware of in Java is a object or a member of an object, including static methods and variables, arrays, and enums. I'm not saying that is definitive, but that's how I understand it...

  19. #19
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    From what I've been taught is similar to what OrangeDog stated.
    Java Code:
    ClassA a = new ClassA();
    ClassA would be the object, however you cannot do anything with it until you create an instance of ClassA.

    I think there's a little bit of a double meaning in how it's defined.
    ClassA is an object and not an instance; however 'a' is an instance of ClassA and at the same time an object(which is why OOP concepts can be used on 'a'). A little confusing but I hope it helps.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  20. #20
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Quote Originally Posted by Steve11235 View Post
    As far as statics, I don't believe the data is stored any differently than anything else. A static variable and an instance variable can both point at the same object on the heap...
    Yes, but the actual pointer will be in the static data segment for the static variable, and in an object on the heap for the instance variable.

    There is a difference between the pointer and what is pointed at (which Java will never let you see).

    Quote Originally Posted by Steve11235 View Post
    The JVM maintains the stack and the heap, not the app. In fact, there really are no "applications" in Java.
    What else would you call an application of code to solve a particular problem. A Java application runs on a virtual machine, and that is the memory layout used by Sun's 32-bit x86 Windows implementation.


    Anyway, the bottom line is - an object is an instance is an object. Instance better describes the process, while object describes the... object. Instantiate is also much more sensible than objectify.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  2. [SOLVED] printing address of current instance of object?
    By emceenugget in forum New To Java
    Replies: 1
    Last Post: 02-09-2009, 09:36 PM
  3. [SOLVED] Non-synchronized instance method of an Object
    By piyu.sha in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-06-2008, 06:35 AM
  4. New Instance for SWT
    By srinivasa_v in forum SWT / JFace
    Replies: 1
    Last Post: 08-08-2007, 01:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •