It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Passing variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing variables" Watch "Passing variables" New topic
Author

Passing variables

Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
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.

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
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
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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 ]

GenRocket - Experts at Building Test Data
Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing variables
 
Similar Threads
Static reference for object
Calling already instantiated objects
Inheritance Variable assignment
Execute thread from another class?
Why is this wrong?