It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Member Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Member Variable" Watch "Member Variable" New topic
Author

Member Variable

Ram Han
Ranch Hand

Joined: Feb 26, 2002
Posts: 49
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 ?


SCJP,SCWCD,IBM 285,MCTS
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
And.j.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
vipul patel
Ranch Hand

Joined: Oct 16, 2005
Posts: 146
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

Joined: Dec 21, 2005
Posts: 70
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

Joined: Nov 08, 2005
Posts: 110
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

Joined: Dec 21, 2005
Posts: 70
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

Joined: Nov 08, 2005
Posts: 110
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

Joined: Dec 21, 2005
Posts: 70
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++



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Member Variable