CrazyEngineers
  • radha
    radha

    MemberOct 15, 2014

    Object and instance in java

    I just want to ask that if i have a class A,and i write

    1. A sup;
    2.A sup=new A();

    Then just tell whether I am correct or not,According to me ,the first line indicates that there is a reference variable sup which is allocated in memory,and it is having value null.
    And if I talk about 2 line then there is a creation of object and its reference value would be given to reference variable sup,so actually what is instance,as it is said that "an object is an instance of class",so is it that the line new A();is representing an object and instance both or is there some marginal difference between them.
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • micheal john

    MemberOct 15, 2014

    I think you got confused between the terms, both object and instance means same.
    Are you sure? This action cannot be undone.
    Cancel
  • simplycoder

    MemberOct 16, 2014

    #-Link-Snipped-#

    Nice explanation, worth a read.
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 17, 2014

    micheal john
    I think you got confused between the terms, both object and instance means same.
    Actually i studied the stack-overflow discussion,in that i read that the reference variable is actually called an instance ,plz clarify it through an example.
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberOct 17, 2014

    Object: A real memory address which exists.

    Instance: A reference which points to an object.

    Variable xyz is an instance of class Integer.
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 17, 2014

    Anoop Kumar
    Object: A real memory address which exists.

    Instance: A reference which points to an object.

    Variable xyz is an instance of class Integer.
    Okay,but then why object is called the instance of class when they both are different.
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberOct 17, 2014

    Actually they both are same it just a way to say this.
    Let see below things in different way.

    Creating a new object.
    Instanciate a class.

    Generally when we create first object of a class we say instanciating it.
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 17, 2014

    Anoop Kumar
    Actually they both are same it just a way to say this.
    Let see below things in different way.

    Creating a new object.
    Instanciate a class.

    Generally when we create first object of a class we say instanciating it.
    Okay I understood,but do you agree than that the memory location of both the instance and the object are same,since the instance is actually a reference and hence it is allocated in the stack while the object is actually allocated in the heap,elsewise an instance actually means that at a particular instant the creation of object with some particular atrributes of class,so am i rght or nt???
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberOct 17, 2014

    They both are synonym....
    If you are talking about reference variables then.
    Objects goes in Object heap and variables goes in stack..
    #-Link-Snipped-#
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberOct 18, 2014

    Anoop Kumar
    They both are synonym....
    If you are talking about reference variables then.
    Objects goes in Object heap and variables goes in stack..
    #-Link-Snipped-#
    Okay, but when i write class obi=new class(),so here obi is the instance or we can say that it is a reference variable so it will obviously go to stack then that means object and instance have both different memory locations or not..
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberOct 19, 2014

    If they are synonym, we use either of them as per convenience just for notion.
    There is no difference in memory allocation.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register