1

I know that non-static variables cannot be referenced from a static context. I want to know why the Java designer made this restriction.

I have checked Stack Overflow, and there are so many similar questions and answers, but I want to know the exact reason.

2
  • 5
    Because it would be nonsensical to access a nonstatic variable from a static context. There's no instance of the object to get the variable from. What you're asking for doesn't make any sense. Apr 26, 2015 at 4:24
  • thanks louis, now i got your point There's no instance of the object to get the variable from
    – kavi temre
    Apr 26, 2015 at 5:49

2 Answers 2

6

So a non-static variable means that an object is required for that variable to have a value. So for example if you have a Car class and you have the non-static variable price it wouldn't make sense that price can be simply referenced by Car.price. That would be like if I asked you "What is the price of a car?". You would tell me that it varies. It would be logical that different cars would be different prices. So if I make a Car BMW then its price would be different than the Car Honda. Now why would you have a static variable? Well if you have something that is true for all cars like say the tax on them. It could also be something you would want to know without having a car, like say the description of what a car is. That doesn't mean that it can't be changed it just means that is same for all instances of that class.

4
  • Lubed i got your point a non-static variable means that an object is required for that variable to have a value. but as per your example tax on the car is a common property and it is possible that it is different for different car. so may point is that there is a facility of abstraction (Interface) for the same.
    – kavi temre
    Apr 26, 2015 at 5:28
  • What exactly are you asking? Apr 26, 2015 at 5:33
  • why java designer make restriction that price of car cannot be referenced from static context. if there is no object of car then you can provide a null value for the price ?? is there is any specific reason behind it
    – kavi temre
    Apr 26, 2015 at 5:35
  • Well imagine it from the computer's perspective. I make a Car BMW and give it a price of 30,000 and I make a Car Honda and give it a price of 15,000. Now when I tell the computer I want to get the value of Car.price is the price really null? There are prices that have values but I am not specifically telling the computer which one I want. Also a primitive data type cannot be null. By making it an error, the computer is telling me that something about my logic is wrong. If it set the value to 0 and didn't tell me something was wrong I might accidentally give away a BMW for free! Apr 26, 2015 at 5:44
1

This is like asking why the universe restricts you from breathing in deep lungfuls of air in outer space. It's impossible because the vacuum of space is defined by the absence of air, not because some designer chose to prevent you.

Likewise, in a static context there are no non-static variables. (It's right there in the name.) You can't refer to a non-static variable in static context any more than you could read a book that hasn't been written.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.