File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Member Variable

 
Ram Han
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class And{
final int i=10;
static int j=100;
public And(){
j=1;
}
public static void main(String a[]){
int j=0;
System.out.println(j);
}
}



It prints 0;

How do i access the member variable j ?
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And.j.
 
vipul patel
Ranch Hand
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can not use "this" reference to access static members. Also "this" reference is not available from static method.

In given example, main method is static and also j is static. so

this.j will not compile.

Only way is access using class name.

And.j will compile and correctly access member variable j.
 
vivekkumar sharma
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by MuraliRam Narasimhan:
[QB]class And{
final int i=10;
static int j=100;
public And(){
j=1;
}
public static void main(String a[]){
int j=0;
System.out.println(j);
}
}

QB]



Hi All,

Reason above code compiles (setting static member inside constructor) ,I believe is because it is legal in Java to access static members via class instances.

Please correct me if I am wrong.

Can someone tell when it will be useful to access static members via instances?
. I don�t think it is a good practice and compiler also gives a warning if you try to do that.


Does someone no what is the reason Why Java has not put restriction on accessing static variables through instances of class?

Regards
 
Krishna Latha Grandhi
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

I don't know the reason for unrestricted access of static variables through instances of class.

But it is legal to access through instances.

And coming to the above code..It is a concept of shadowing...

if didn't shadow static variable.. we can access that variable directly.

Here the variable is shadowed.

Here we can access the variable by using instance of the class or by using the classname.staticVariableName....

Hope this will help...

Regards,
Krishna.
 
vivekkumar sharma
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hari Krishna:
Hi friends,

Here the variable is shadowed.

Here we can access the variable by using instance of the class or by using the classname.staticVariableName....



Regards,
Krishna.



I donot agree the variable is shadowed.
Do we have variable shadowing in JAVA.(can u give an example)
Above is not variable shodowing.if it is can u explain how ?
 
Krishna Latha Grandhi
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi vivek,

Yes. shadowing concept is in Java.
Shadowing is nothing but hiding the original variables with the other local variables having same name.

I think you saw the similar code often..



See the above code Actually the values of var1 and var2 are not changed.
The same thing happend in the above code, the instance variable j is hidden by the local variable j.
 
vivekkumar sharma
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hari Krishna:
[QB]Hi vivek,

Yes. shadowing concept is in Java.
Shadowing is nothing but hiding the original variables with the other local variables having same name.

I think you saw the similar code often..


Yes, I agree with what u have said about Shadowing ,BUT i disagree with the example u have given.And this also applies to the original code example.

Beacuse both the variables r in different context one is class filed and other one is local variable to function. they are stored and handled in different way as well.

Following examples i think clarify shadowing better and
are not legal code in JAVA, where as they r allowed in C/C++



  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic