wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Inner not knowing about outer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner not knowing about outer" Watch "Inner not knowing about outer" New topic
Author

Inner not knowing about outer

Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
I have, through the kind help of some of the people here, figured out some of my problems in learning swing. But, now, I am having trouble with getting an inner class to see objects in the outer class. What is wrong with my syntax that is causing this? Thanks in advance for your help.

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

It's because you've defined mainButton inside the constructor instead of defining it globally for the class. So it is only visible in the TestMainScreen constructor method.


GenRocket - Experts at Building Test Data
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

mainButton is a local variable declared in the constructor of the outer class. Local variables are only visible inside the method that declares them. If you want MainButtonListener to see it, make it into a member variable of the class -- i.e., declare it at class scope, and then assign it in the constructor as now.



[Jess in Action][AskingGoodQuestions]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

With that being said, several of your variables are going to have the same issue because they are in declared in the constructor.

uNameField
pWordField

Will both need to be made global to the class.
Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
Then this should do the trick?



Thanks Greg and Ernest!
[ May 23, 2005: Message edited by: Al Wells ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Al Wells:
Then this should do the trick?



Thanks Greg.


Yep, that should pretty much do it. Note also that your JOptionPane.showMessageDialog argumaents are missing the Dialog Title.

Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
I noticed that when I just compiled. I am fixing now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner not knowing about outer