user1751510
user1751510

Reputation:

Java static methods variables resolved during compile time, loaded during run time?

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?

Upvotes: 2

Views: 3129

Answers (1)

Steven
Steven

Reputation: 2477

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.

Upvotes: 8

Related Questions