This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Can someone please explain local variables inside a static method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can someone please explain local variables inside a static method?" Watch "Can someone please explain local variables inside a static method?" New topic
Author

Can someone please explain local variables inside a static method?

Oliver Chase
Greenhorn

Joined: Feb 09, 2010
Posts: 15
Hi,

I'm a bit confused here. Basically, I have two static methods, reverse() and reverseWords(). I have declared a String answer inside reverse() and a String answer inside reverseWords().

Why does the second answer variable inside reverseWords() not get recognized? Is this second answer variable a static variable implicitly b/c it's inside a static method? Or is it a local variable whose scope is just inside the reverseWords() method? Is there two copies of the answer variable, one for each method, or just one for the entire class?

Thanks

Charles Mulloy
Ranch Hand

Joined: Oct 25, 2009
Posts: 30
Simply going "String answer;", is not enough. You never constructed 'answer'.

I think that's what is wrong. What are you trying to do?
Oliver Chase
Greenhorn

Joined: Feb 09, 2010
Posts: 15
Charles Mulloy wrote:Simply going "String answer;", is not enough. You never constructed 'answer'.

I think that's what is wrong. What are you trying to do?


Thank you Charles!

So maybe I just have a general question...

Are local variables declared inside a static method considered local and static? Could another static method access my local variable?

like

method2() is referring to the x that is used in method1(). But x is local. But x is also static implicitly (I think), which means that there is one copy of x per class? Does my confusion seem to arise from some rational thought?
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

Welcome to Javaranch!

puff tissue wrote:

Please change your display name to use your real first and last names. Obviously fictitious names are not allowed under JavaRanch's Naming Policy.


Now for your query,
puff tissue wrote:Why does the second answer variable inside reverseWords() not get recognized? Is this second answer variable a static variable implicitly b/c it's inside a static method? Or is it a local variable whose scope is just inside the reverseWords() method? Is there two copies of the answer variable, one for each method, or just one for the entire class?


What do you mean by "not get recognized"? What is the error that you are getting?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

"puff tissue", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80


The static method can be directly called on the class instead of the class object. But still the variables declared inside the method are local variables which reside on the stack. If it would have been a class level static variable then it would have been visible in method2(). So your variable x here is a local variable to the static method method1() and hence it is not accessible in method2()




Thanks and Regards,
Suhas
http://www.xplore-java.blogspot.com/
Oliver Chase
Greenhorn

Joined: Feb 09, 2010
Posts: 15
Thanks Suhas! I understand it now.
Charles Mulloy
Ranch Hand

Joined: Oct 25, 2009
Posts: 30
My Professor explained it like this. "Methods are greedy and don't like to share." Consider the following code, (assume there is a main() method somewhere running these methods. It's late where I am, and I feel lazy):


If a variable is declared and constructed in a method, then other methods cannot see them. The above will get an error. But consider this code:



The way I used the variables above allows any method to use them, because of the way they where declared. "z" was declared, but not constructed. methodThree() constructed and then used it. One last trick:



In the above, methodOne() called methodTwo() and passed "x" to it. methodTwo() "received" the value as "out". This is a convenient way to quickly pass a value to another method without making the value global.

I hope this helps. (And that I didn't go overboard...)
Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80
Thanks Suhas! I understand it now.


You are welcome Oliver
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can someone please explain local variables inside a static method?
 
Similar Threads
error with JSP and Bean
string reversal problem
static variables inside functions
inner class question
doubts regarding java.util.Arrays