• 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

Question ID :957734680770

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys
I can't understand why this compiles "correctly" and donot give error of redeclaring of "i".
static int x = 5;
public static void main(String[] args)
{
int x = ( x=3 ) * 4;
System.out.println(x);
}
Regards Denish.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java allows you to redeclare variables, but you need to be careful about the scope. The x local to the method is only valid within the method. You can access the class variable x within the method by using this.x.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Lance is correct (the non-static x is local to the main method and is therefore not the same as the class variable x), except that since the class variable is static, you can't access it with the this keyword, which references an instance of the class. The class name is not supplied, but you would reference the static variable x as Classname.x rather than this.x.
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scott,
You have rightly summed it.
"this" is not accessible to the static member of a class as it is the instance variable.
Ravindra Mohan.
 
Lance Finney
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot that main itself was static. Wouldn't my statement be true if the method weren't static?
For example, the following code returns 1 and 0. Although the variable is static, "this" has a link to it.
public class Test
{
static int i = 0;
public static void main(String[] args)
{
Test t = new Test();
t.amethod();
}
public void amethod()
{
int i = 1;
System.out.println(i);
System.out.println(this.i);
}
}
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lance Finney:
I forgot that main itself was static. Wouldn't my statement be true if the method weren't static?
For example, the following code returns 1 and 0. Although the variable is static, "this" has a link to it.
public class Test
{
static int i = 0;
public static void main(String[] args)
{
Test t = new Test();
t.amethod();
}
public void amethod()
{
int i = 1;
System.out.println(i);
System.out.println(this.i);
}
}


Yes because non static members have access to static members but not vice versa.
Vivek
 
denish mehta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a lot guys for such a good explainations
Denish
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scott Appleton:
since the class variable is static, you can't access it with the this keyword, which references an instance of the class. The class name is not supplied, but you would reference the static variable x as Classname.x rather than this.x.


This is incorrect. Static variables can always be accessed through an instance of the class but you still get the one and only static variable.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic