File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about protected n static" Watch "about protected n static" New topic

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?
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

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:
subject: about protected n static
It's not a secret anymore!