• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing variables

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question about how to pass a variable in the outer class to an inner class for use. I have used a println function to see that my variable is null when I try it as it is shown below. However, it prints out fine in the constructor and the main class itself. The variable is comming from another object but I want to continue to use it as I instatiate even more objects. Right now I am solving this problem through a clunky work around. Hopefully this makes sense.

I created an example of what I am try to do in order to clarify.

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two subtle things wrong with your example, and I suspect that straightening them out would answer your problem -- or at least put someone in a position to answer it.

The first one is that "myVar" is an instance variable of MyClass, but you use it in main() as if it's a static. Which is it?

The second one is that myVar isn't assigned to anywhere in this example. Where is it assigned to?

One extremely common mistake -- which I suspect might be the problem here -- is "shadowing" an instance variable with a local variable. For example,



This assigns to value of the parameter to itself, and leaves the instance variable uninitialized.
 
Al Wells
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest,

Thanks for the help. I must admit that I am still a little in the dark (no surprise there).

myVar is not static. It is passed from another class. When I tried creating classes without myVar in main, I got constructor errors in the compiler. Granted that I only have a main in order to compile and run each class on its own just so that I can make sure that I have no syntax errors. In this example, I do have a couple. I would have to have String myVar declared in main as well in order for the compiler not to throw an error when I try to javac the file. I also should have written new MyClass(String myVar) and then public MyClass(String myVar) where myVar is created and initialized in some other class which calls this constructor. That is only learned from having tried it the other way.

public SomeOtherClass {

String myVar = "The value";//here it is initialized

MyClass passMyVar = new MyClass(myVar);//here it passes fine.

}//close class

public MyClass{

String myVar;

public MyClass(String myVar) {

//myVar is intialized here but not in inner class. But, if I pass it here it is fine.
MyOtherClass passMyVarAgain = new MyOtherClass();
passMyVarAgain.getReturnValue(myVar) //passes fine with the value that I expect it to have.

}//close constructor

private ActionListener {
public method {
MyOtherClass passMyVarAgain = new MyOtherClass();
passMyVarAgain.getReturnValue(myVar); //myVar is now null. But, if not declared as an instance variable for the class, I get a compiler error.

}//close method
}//close inner class
}//close class

Does that make some sense? I know, I can see you laughing...

All of this is due to my inexperience and learning on the fly I am very sure. myVar actually gets initialized in another class where it is returned and then passed to this class. So, I thought that myVar is automatically assigned the value that is returned from the other class. Now I want to pass that value to yet another object in order for that object to do what it does. Do I need to initialize another variable in the constructor and then pass that variable? Something like String myOtherVar = myVar (I gather from your response that this is not right either)? Or is there some other way that I need to go about it?

Thanks for your help, Ernest. I am sure that I will get this with a little more help. Most of the items that you have helped me with I have learned quickly but this one is giving me a little more trouble.

All the best,
Al
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I might see your problem



At this point you have 2 myVar variables. And they both have 2 different values. The instance variable you declared for the class is null. The variable you declared as an argument in your constructor has a value. Try doing this and see if it resolves your issue with the passing of the variable in the inner class.



Here you are assigning the class variable the value passed to the constructor. And that class variable is what you are using in your inner class (action method).
[ June 11, 2005: Message edited by: Gregg Bolinger ]
 
Al Wells
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that I have said this to you before but, you are the man. So, now I know. I have to re-declare the variable explicitly in the constructor if that is the only place that it occurs and I want it to be usable by an inner class and I don't have a method where it is passing. That works just like it should.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic