• Post Reply Bookmark Topic Watch Topic
  • New Topic

Total confusion b/w reference & object  RSS feed

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a method we pass reference ( not actually reference , content of reference ) or object ...

I know we pass content of reference like :

method :
static void testMethod(Dog d) {

}

how to call :
Dog myDog = new Dog();
testMethod(myDog);

But why this doubt came in mind is from addActionListener() method .
myButton.addActionListener(new Act());

class Act implements ActionListener {
// actionPerform() method
}


please solve my doubt ...
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object (or instance of a class) is the actual object in memory. A reference is a pointer to that object. References are stored in variables while objects just sit in memory somewhere. Compare this with integers. An integer variable holds the actual integer value completely; it doesn't point to the value.

To clear your down, note that "new Act()" returns a reference to the instance of Act that gets created. In your example, both calls are passing a reference to the object.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it means
Act a = new Act(); // new Act(); will return the bit pattern ( the content of reference a ) & this also make an object of class Act that is on GCH .
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a few minutes to read this and this. Both entertaining and informative -- should clear everything up for you.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK but what was wrong in my statement . or can we say it like this
new Act(); will return a remote control & make a object on heap .

is this right ?

thanks .
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing was wrong with your previous statement -- I just thought you'd like to read the Campfire Stories.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!