File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Child extends Parent, Belongs to same package, Cannot access public 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 "Child extends Parent, Belongs to same package, Cannot access public variable" Watch "Child extends Parent, Belongs to same package, Cannot access public variable" New topic
Author

Child extends Parent, Belongs to same package, Cannot access public variable

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Hello,

I have two programs based on code from Sierra/Bates Chapter 1:

location: /one/src/Parent.java



location: /one/src/Child.java




I navigate to one directory and type in javac -d ./classes/ ./src/Parent.java ./src/Child.java. Below is the error:

non-static variable x cannot be referenced from a static context
System.out.println("x is " + x);



One would think the code compiles and executes considering that Child inherits from Parent, Child is in same package as Parent, and the variable is PUBLIC.





Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The problem is that you are accessing the instance variable from the main method, which is a static method. Read the error message again : non-static variable x cannot be referenced from a static context. I don't know if you have already studies what static variables and static methods are. Try to make a public non static method in Child, and try System.out.println("x is " + x); again.


[My Blog]
All roads lead to JavaRanch
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Hello sandra Bachan , this is the most frequent mistake encountered by JAVA learners.

Just to follow one rule :

Non-Static members cannot be accessed from Static context ie Static methods directly without referring to any object.

Reasoning :- Static members could exist before object creation.

So when you are trying to access variable x which is inherited from the parent class, it is still non- static. You try to access a non-static variable from the static main method and hence the error. Because at that time if suppose,say, 100 objects of the child class already exists on the heap, then compiler complains because it does not resolve that whose x are you trying to refer .

But But But if you say i am referring to object number ,say 75, then you would say ob75.x , now compiler knows that you are referring to x which exists inside object75, and hence it is allowed.




SCJP 6.0 96%

(Connecting the Dots ....)
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
All that said, you have two options to make that work

One way, make x static:



Second way, instantiate Child or Parent and invoke the call on the object:




Hope this helps


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
These are all very helpful and now my code works.Thanks guys!

@ Sahil: I really appreciate you including the reasoning, i.e. static members can exist before object creation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Child extends Parent, Belongs to same package, Cannot access public variable