aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about protected n static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about protected n static" Watch "about protected n static" New topic
Author

about protected n static

ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
hello, why when i put static in a method i can't access to the protected method, but when i take away the static i can..below is the code...

package certification;

public class Parent {
protected int x=9;
}

package other;
import certification.Parent;
public class Child extends Parent{
public static void main(String[] args) {

}
public static void shpw(){ //Over here when i remove this static it works..why?
System.out.println(x);
}
}
Sreeraj Harilal
Ranch Hand

Joined: May 09, 2006
Posts: 45
static methos cannot access non-static variable.


SCJP 5.0<br />SCWCD 1.4<br />SCBCD 1.3<br />SCEA part 1
Terry Huang
Greenhorn

Joined: May 16, 2006
Posts: 25
for the "proteced int x=9" to be initialized, the constructor must be called (in other words, the object must be created). A static method however can be accessed without having the object be created. So if you call a static method that has a instance variable, what value will that variable hold in the method? That is why the compiler doesn't allow you to use non-static variables in static methods.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about protected n static