• Post Reply Bookmark Topic Watch Topic
  • New Topic

A class calling an internal method without an instance ?  RSS feed

 
bob von ilten
Greenhorn
Posts: 21
Linux Mac OS X MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In one of the HFJ projects (BeatBox) the following line is the only thing in main() "new BeatBox().buildGUI();". Am I correct in that there really is no instance or object of type BeatBox being created?

Further on in the lesson we are asked to implement JFileChooser. If main is left as is and JFileChooser is implemented via an internal listener class then this code will fail



if you edit main as follows then the above code will work.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob von ilten wrote:In one of the HFJ projects (BeatBox) the following line is the only thing in main() "new BeatBox().buildGUI();". Am I correct in that there really is no instance or object of type BeatBox being created?



Of course an instance of BeatBox has been created -- what do you think the "new BeatBox()" part of the expression does?

Henry
 
bob von ilten
Greenhorn
Posts: 21
Linux Mac OS X MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that is part of what I am trying to understand.

if new BeatBox().buildGUI(); creates an instance how is it addressed what is it reference?

with

BeatBox BeatBoxFc = new BeatBox();
BeatBoxFc.buildGUI();

I know that there is an object created of type BeatBox with a reference value of BeatBoxFc
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BeatBoxFc is a reference variable pointing to the created object. Oh, and it's the new operator which creates the object, not the buildGUI() method!

Btw. it should start with a lower-case letter in Java ;-)

Marco
 
bob von ilten
Greenhorn
Posts: 21
Linux Mac OS X MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, thank you I understand that new is what creates the object and that BeatBoxFc is then the reference variable.
If you use this code though, "new BeatBox().buildGUI();" what then is the reference value?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you chain the call on buildGUI() to the call to the new operator then there isn't a reference variable. You can call buildGUI() directly on the result of new which is the object new created.

In your case it probably won't be very helpful without a reference to the object. If there aren't any side effects which result in reference to this object it will simply be garbage collected soon after you've created it.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob von ilten wrote:If you use this code though, "new BeatBox().buildGUI();" what then is the reference value?

Think of it as an anonymous, one time use reference. It's used to call the buildGUI method and then disappears. If you want to call a second method or need a reference to the object for anything else, then you need to split it into two lines in the way you're used to.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob von ilten wrote:Yes, thank you I understand that new is what creates the object and that BeatBoxFc is then the reference variable.
If you use this code though, "new BeatBox().buildGUI();" what then is the reference value?


You seem to be assuming that an object is somehow inherently bound to a specific reference variable, or that it has to have a variable associated with it. That's not the case.

When we do new Something(), that creates an object, and returns a reference to that object. So new Something() is an expression that has a type of "reference to Something." We may happen to assign the reference that is the value of that expression to a variable: Something sth = new Something(), but we don't have to. And even if we do, there's no inherent tie between the object and the variable. The object doesn't know about the variable, and if we later point that variable at a different object with sth = someOtherSomething or set that variable to not point to any object with sth = null, that has no effect at all on the object, other than possibly making it eligible for GC.

Going back to what I said earlier, new Something() is an expression of type reference-to-Something, so it can be used anywhere an expression of that type is valid. If we could to otherMethod(sth)[tt/] or [tt]sth.somethingMethod(), then we can do otherMethod(new Something())[tt/] or [tt]new Something().somethingMethod(). The only difference is that since we didn't assign a variable at construction time, we can no longer use that newly created object. It still exists though, until there are no reachable references to it. In particular, when we call otherMethod(new Something()), that method may assign the value passed in to a variable for later use.
 
bob von ilten
Greenhorn
Posts: 21
Linux Mac OS X MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I get it I think. So if we do not have a variable tied to the reference of the created object would that be a case for "this"

BeatBox().buildGUI();


JFileChooser fc = new JFileChooser();
fc.showOpenDialog(theFrame.this);

or

JFileChooser fc = new JFileChooser();
fc.showOpenDialog(BeatBox.this);

depending on the enclosing object?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob von ilten wrote:Ok I get it I think. So if we do not have a variable tied to the reference of the created object would that be a case for "this"p.


Not really. Whether "this" is present or not present in a given context is not affected at all by whether you assign the value to a variable.

I think you're thinking it's more complicated than it really is. You crate an object. Then you either keep a reference to it in a variable so you can continue to access that object, or you don't.


BeatBox().buildGUI();


JFileChooser fc = new JFileChooser();
fc.showOpenDialog(theFrame.this);

or

JFileChooser fc = new JFileChooser();
fc.showOpenDialog(BeatBox.this);

depending on the enclosing object?


No. Assuming theFrame is a variable, not a class name, I don't think that's even legal. I don't think you can do variable.this, although I might be wrong. The second case, SomeClassName.this is used in a particular situation, and has nothing to do with what I've been talking about so far (and what I assume you've been asking about so far). It's used when you create an instance of an anonymous inner class, and you want to refer to the current instance ("this") of the enclosing class, rather than the instance of the anonymous inner class.

I don't know if this applies to your current situation and question or not. But if you're not 100% solid on objects and references and variables for the simple, general cases I talked about previously, then anonymous inner classes and the various this-es involved will be extra confusing.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote: . . . SomeClassName.this is used . . . when you create an instance of an anonymous inner class, and you want to refer to the current instance ("this") of the enclosing class, rather than the instance of the anonymous inner class. . . .
I thought it was used inside an instance of any kind of inner class.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Jeff Verdegan wrote: . . . SomeClassName.this is used . . . when you create an instance of an anonymous inner class, and you want to refer to the current instance ("this") of the enclosing class, rather than the instance of the anonymous inner class. . . .
I thought it was used inside an instance of any kind of inner class.


You're right, it is. I guess I just got hung up on anonymous becuase I've been using a lot of them lately, with precisely that EnclosingClass.this idiom.
 
bob von ilten
Greenhorn
Posts: 21
Linux Mac OS X MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guy I appreciate patient assistance.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!