2

I was reading about the static methods and variables in Java and I found the below two articles on stack overflow.

Are static methods in Java always resolved at compile time? This says, static methods are resolved at compile time.

when is static variable loaded in java, runtime or compile time? time-or-compile-time

This says, static methods are loaded at compile time.

Shouldn't the compiler load the static variables/methods when it resolves them(say at compile time)? It's Confusing! Could some one please clarify?

Community
  • 1
  • 1
  • 2
    Well it does. Both the answers says `Compile-Time` only. What are you confused at? – Rohit Jain Oct 16 '12 at 22:39
  • If you want a method/class be resolved at the time it is really needed you will have to create an Object. But if you still want to have a static like acces, you can go with the sinlgeton pattern, descriped here: http://en.wikipedia.org/wiki/Singleton_pattern – Robin Oct 16 '12 at 22:42
  • why do people care about compile/run time? it's impl detail. – irreputable Oct 16 '12 at 22:56

1 Answers1

8

It seems to me like you are confused about what the terms "resolve" and "load" mean.

Resolving a method/variable means deciding exactly which method/variable is called. For instance methods for example this is done at runtime, which results in the ability of a subclass to override a superclass's methods (polymorphism). Static methods however cannot be overridden and are resolved at compile time.

Loading a variable means actually getting the value into memory. Of course, this can only happen at run time. Specifically, a static variable is loaded when the class itself is loaded.

Steven
  • 2,437
  • 5
  • 32
  • 36