• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

no static??

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class ScopeTest
{
static int x = 5;
public static void main(String[] args)
{
int x = ( x=3 ) * 4; // 1
System.out.println(x);
}
}
The Answer is : 12
But in the variable declaration the value of int x is 5 which is also static, then how come 'x' is getting the value 3 in the main.
I feel it should give a compile error.
Please explain me
Sonir
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static is NOT final, you may change the value of a static variable whereas you cannot change the value of a final variable...
HIH
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Valentin.
i agree with your answer but i have a question myself.
in this case in the main method he didnt only assign a new value to x. but he declared a new int.
in the main there is also int x.
thus he declares a new local variable in the method main.
how does the compiler know which x we refer to in the main method (the static x or the local x).
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good point. yes the main method is infact defining a new integer.
the System.out.println method is referring to the local variable x.
if you were to refer to the static x then refer it to via Class_name.x .

Originally posted by Roy Ben Ami:
Hi Valentin.
i agree with your answer but i have a question myself.
in this case in the main method he didnt only assign a new value to x. but he declared a new int.
in the main there is also int x.
thus he declares a new local variable in the method main.
how does the compiler know which x we refer to in the main method (the static x or the local x).

 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep the x variable in the main method "shadows" the variable definition of the class. See the following thread about a discussion on that:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014301
HIH
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe it is due to the context.
'x' normally in a non-static context refers to this.x. Sine static doesnt have this, 'x' refers to the static value of the 'x'. That value is shadowed (correct me if i am wrong) by the local variable 'x' , resulting the value to be 12
ScopeTest.x will print the value of x = 5 no matter where it is called...
Ragu
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys
got it.
 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..
 
Run away! Run away! Here, take this tiny ad with you:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic