• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

About object ,reference and class variable

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt about object,reference and class variable.

1.reference has a memory address of object and object can hold more than one value as it's an array.But when we create a object,a constructor called and it set value of class variable.then another obj. also set another values of class variable. then what exactly the relation among these three.if is there is any diagram which explain it better then please send me the link.I am confused in this example because of these three things.

 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sure someone will answer this better than me, probably before I finish typing, but here goes:

You have two kinds of memory: stack and heap. Your local method primitives (int, boolean, float, double, etc) and references are created on the stack, and objects are created in the heap. OK, for literals, this is quite easy. If we have a primitive int, it puts the value 4 in four bytes at the top of the stack. If your program wants to read that value, it just looks at that position in the stack. If it wants to change the value, that's OK too. Once the method is finished the stack is popped and everything it stored there is gone.

What if you instantiate an object though? The object is created on the heap, a totally separate area of your memory. If you don't assign it to a local variable with =, then it is still there, but it's lost to your program, and will eventually be reclaimed by the garbage collector. Normally though you do assign it to a variable like you do with "Test obj1 = new Test(2)". That creates a Test object on the heap, and puts a reference to it on your stack. The reference does two things: it tells your program where to find the actual object in the heap, and it tells the garbage collector that someone is still using this object, so hands off!

In your program code, you create three Test objects, but only have two references to them. First you create one (on the heap) with the value of 2 and assign its reference to obj1 (on the stack). Then you create a second Test (also on the heap) by calling the incrByTen() method, and assign that to ob2 (also on the stack). Finally, you call incrByTen() again, creating a third Test instance, and assign that to ob2. The reference to the second Test instance, the one that has a value of 12 is now gone. The object still exists on the heap, but you have no way to access it, and the garbage collector will eventually get around to destroying it.

Once your main method returns, the stack containing ob1 and ob2 is popped, and then the references to all the Test instances would be gone. If that weren't the end of your program, they would still exist on the heap until the garbage collector ran.
 
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic