• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.




 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic