• 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

Static variable

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Check the following code,
I thought, since sName is a static variable the ans would be "Joey01",
but it is "Joey" - i am surprised, could someone explain
thanks in advance.

Edited by Corey McGlone: Added CODE tags.
[ June 11, 2003: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mari,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sName is a local variable in runM0() and runM1() methods. So this variable hides your static sName. Therefore output is "Joey"

Originally posted by mari:
Hi all,
Check the following code,
I thought, since sName is a static variable the ans would be "Joey01",
but it is "Joey" - i am surprised, could someone explain
thanks in advance.
public class Tux{
static String sName = "Joey";
public static void main(String argv[])
{
Tux t = new Tux();
t.runM0(sName);
System.out.println(sName);

}
public void runM0(String sName)
{
sName = sName + "0";
runM1(sName);
}

public void runM1(String sName)
{

sName = sName + "1";

}

}


[ June 11, 2003: Message edited by: rahul V kumar ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you modify the code in the methods to look like this:

You'll get the results you expected.
I hope that helps,
Corey
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mari
The copy of a reference to the variable sName is being passed to the method runM0(String). What the method is instead doing is shadowing( or hiding) the class variable sName. If you change the method variable name to something else then the output will be as you expected. If the method is like public void runM0(String notsName) and public void runM1(String notsName) then the output had been what you have said.
 
mari krishna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also is it safe to say that, since sName is static and it is shadowed during the modifications. These modifications would not be reflected at t.runM0(sName), and System.out.println(sName). Basically static variables get priority, when they are called before any kind of instance method modification. Please correct me if I am wrong !!!
Thanx,
Sefa Urgenc
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sefa
With variables there is no priority issuse concerned. There is hiding or shadowing. For your first part

Also is it safe to say that, since sName is static and it is shadowed during the modifications. These modifications would not be reflected at t.runM0(sName), and System.out.println(sName).


Since no modification was done, no modification will be reflected. System.out.println(sName) would simply print out sName value.
[ June 12, 2003: Message edited by: Anupam Sinha ]
 
Sefa Urgenc
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anupam,
But what about the sName in the runM0 and the runM1 methods, are they being shadowed ? And also if the sName was private, and we were trying to set the value of sName through public. Does this mean we have to use this keyword with sName, so the value would be modified.
Thanx,
Sefa Urgenc
 
Sefa Urgenc
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I meant to say public methods, to tightly encapsulate
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sefa
Let's go statement by statement


But what about the sName in the runM0 and the runM1 methods, are they being shadowed ?


The variable that is being shadowed is sName which has been declared in the class. The variable sName that has been declared in the method runM0(String) and runM1(String) is not being shadowed. Infact the method variable are shadoing the class variable.

And also if the sName was private, and we were trying to set the value of sName through public. Does this mean we have to use this keyword with sName, so the value would be modified.


If sName was private then the value of sName can be set through setter methods. But they needn't be public, a protected or default access level specifier method can also be used. but this is only for other classes declared outside the class Tux. The code inside Tux can freely access sName. So in here there is no question of encapsulation.
Secondly this is used to refer to the instance variable with the associated instance. That is if I say t.method1(); then t is the instance variable now t can access instance variables like t.var1=10; etc.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the link which shows the how paramaters are passed in Java
The applet used to show it is awesome
http://www.geocities.com/mcglonec1978/javacert/paramPassing.html
 
Sefa Urgenc
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amazing applet , thanx Megan and good job Corey.
 
reply
    Bookmark Topic Watch Topic
  • New Topic