• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can someone please explain local variables inside a static method?

 
Oliver Chase
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"puff tissue", please check your private messages for an important administrative matter.
 
Suhas Bilaye
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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()



 
Oliver Chase
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Suhas! I understand it now.
 
Charles Mulloy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Suhas! I understand it now.


You are welcome Oliver
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic