Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.




 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic