Object and instance in java

radha gogia

radha gogia

@radha-BTDzli Oct 24, 2024
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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • micheal john

    micheal john

    @micheal-john-l1fIn3 Oct 15, 2014

    I think you got confused between the terms, both object and instance means same.
  • simplycoder

    simplycoder

    @simplycoder-NsBEdD Oct 16, 2014

    #-Link-Snipped-#

    Nice explanation, worth a read.
  • radha gogia

    radha gogia

    @radha-BTDzli Oct 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.
  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Oct 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.
  • radha gogia

    radha gogia

    @radha-BTDzli Oct 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.
  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Oct 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.
  • radha gogia

    radha gogia

    @radha-BTDzli Oct 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???
  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Oct 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-#
  • radha gogia

    radha gogia

    @radha-BTDzli Oct 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..
  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Oct 19, 2014

    If they are synonym, we use either of them as per convenience just for notion.
    There is no difference in memory allocation.