-2

If I refer non static variable with class name, when one object is there, why it is compile-time error?

I have created one object so one set of non-static variable is there in Heap Area. So if I refer that non-static variable with class name, there is only one non-static variable, so it can use that.

iabhilashm
  • 11
  • 7
  • If you write the java language how would you deal with that? I mean the only thing you have is reference to static class, how would you be looking for object instance in the heap without the reference? – deathangel908 Jun 12 '15 at 05:30
  • It would really help if you would show a short but complete example demonstrating the problem. – Jon Skeet Jun 12 '15 at 05:45

3 Answers3

1

Event if it makes sense, how does the compiler to know that you created only a single instance? static variables belongs to the Class objects (incidentally they are instances of class Class).

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
1

First because that's how it is.

Secondly because at compile time nobody knows whether there will be 0, 1 or a million instances. Besides it would result in extremely confusing code, when you couldn't tell if something is referring to a static variable or the "single instance variable".

Kayaman
  • 72,141
  • 5
  • 83
  • 121
1

Lets say you could do that and there was no compile time error, How could the compiler know, which variable reference when there are more than 1 instances? Putting your theory in practice just for 1 instance would result in nonsensical looking complex code.

If you want more details and need the programmer's perspective, go read the Java Language specification and JVM specification to see how the memory management is specified.

https://docs.oracle.com/javase/specs/

Manish Kumar Sharma
  • 12,982
  • 9
  • 58
  • 105
  • I am trying to ask that, if I refer non-static variable I have to refer with reference variable only. But if I refer by class name (Like ClassName.nonStaicVariable = 100; ), why it is showing error? – iabhilashm Jun 12 '15 at 05:39
  • If I have created 2 or more instances then compiler may confuse which non-static variable of which instance I am trying to refer. But for a single instance it could use that one. – iabhilashm Jun 12 '15 at 05:41
  • Lets say it could, but why change the whole access model just for 1 instance. The resulting code and the rules will be complex. Programming is about solving problems elegantly not dumping a bad solution. – Manish Kumar Sharma Jun 12 '15 at 05:42
  • @iAbhilashM - *static fields* don't hang in *mid air*, they are also part of an Object called the class Object. Even if you have only one instance, you need to tell the compiler that you are accessing an instance field from that instance. – TheLostMind Jun 12 '15 at 05:42
  • Yes, I agree. We can also refer one static variable with reference variable, right? But that is not recommended and it is not showing any error. – iabhilashm Jun 12 '15 at 05:45
  • But in case of non-static variable reference with class name of a single instance, why it is showing error? – iabhilashm Jun 12 '15 at 05:46
  • @iAbhilashM : Why are you keen on attaching the class name with a particular instance? It's simple- Static variables do not belong to any object. They are unique and associated to the class, meant to be accessed at the class level. Separate the notion of accessing non-static and static variables at 2 different levels. Don't mix them up. That will clear your mind. – Manish Kumar Sharma Jun 12 '15 at 05:51
  • Ya, now I got an Idea. I am in beginner level, so this type of confusions are occurring, – iabhilashm Jun 12 '15 at 06:18