Static variable access
Hi everyone,
Just a quick question:
In the following piece of code, why does the compiler NOT complain about non-static methods accessing a static field?
publicclass Plane{
static String s ="-";
publicstaticvoid main (String[] args){
new Plane().s1();
System.out.println(s);
}
void s1(){
try{
s2();
}
catch(Exception e){
s +="c";
}
}
void s2()throws Exception{
s3();
s +="2";
s3();
s +="2b";
}
void s3()throws Exception{
thrownew Exception();
}
}
[1613 byte] By [
lafkiotisa] at [2007-11-27 6:15:26]

> In the following piece of code, why does the compiler> NOT complain about non-static methods accessing a> static field?I think you have it backwards. The complaints come if a static method tries to interact with a non-static variable.
For instance:
class Fubar1
{
private String nonStaticStr = "foo";
private static String staticStr = "bar";
// nonstatic methods can access both static
// and nonstatic variables.
private String nonStaticMethod()
{
return nonStaticStr + staticStr;
}
// static methods can only access static
// variables.
private static String staticMethod()
{
// this doesn't work
//return nonStaticStr + staticStr;
// but this will work
return staticStr;
}
public static void main(String[] args)
{
System.out.println(staticMethod());
System.out.println(new Fubar1().nonStaticMethod());
}
}
Message was edited by:
petes1234
Yeah..that's what I did in the snippet I posted earlier. I removed the static keyword from the variable and str8 away the compiler complain in the main method....It is so annoying that Sun Certificate exam...:P
the static variables are common for all the instances of a class.
the static blocks , static variables and static methods are created when the class is compiled. That's why we can not use the static variables in the non-static methods.
Thats why we can run the class without public static void main(String args[]) method.
please run this
class Test {
static {
Sop("This is sameple text");
}
}
So another another way of seeing it:
In non-static context we have the 'this' reference.
In static context we don't have the 'this' reference.
For accesing a non-static member, we need the 'this' reference.
For accesing a static member, we don't need the 'this' reference.
Can anybody explain a little more about what happens when static classes are compiled, or point me in the direction of some reading material please?
I would like to know the difference between static variables and standard variables at compile time. I am confused because I would have thought that if there is a declaration in the code then the compiler knows that that object will exist.
Thanks
> Can anybody explain a little more about what happens
> when static classes are compiled, or point me in the
> direction of some reading material please?
>
> I would like to know the difference between static
> variables and standard variables at compile time. I
> am confused because I would have thought that if
> there is a declaration in the code then the compiler
> knows that that object will exist.
>
> Thanks
When the class loader loads the class, that's when static instances are created and when static blocks of code are executed. A java Class is actually an Object like any other, so you can think of static variables as instance variables for the Class object, and static blocks of code are somewhat like constructors for the Class object (in the sense that the code is executed when the class is loaded by the class loader).
Individual instances of the object share the class' static variables, but non-static variables are only available to the instances. So the class 'instance' (which was loaded by the class loader) can't access non-static variables because non-static variables don't exist until you create an instance of the class.
> so you can think of static
> variables as instance variables for the Class object,
> and static blocks of code are somewhat like
> constructors for the Class object (in the sense that
> the code is executed when the class is loaded by the
> class loader).
That's not really a good model, and will probably lead to more confusion. Just like saying, "You can think of Java as passing objects by reference."
jverda at 2007-7-12 17:26:05 >

Hi Jverd,
Could you explain it to me then? I understand now from Binary Digit's answer, but if this is not correct then could you provide one? I dont want to go away thinking that I understand, when in actual fact I am wrong.
A link to a good article would be just as good if anyone knows of one.
Thanks