2

Given a class

public class A {
  public static int classVal;
}

During run-time, does the type A become a reference to a class Object?

public static void main(String[] args) {
  A a = new A(); 
  A.classVal = 100;
  A.classVal = 200;
}

if A.classVal is a reference to static Class variable, then is A a reference to an Object that represents the class?

does a.getClass() refer to the same object as A?

To clarify

if A.classVal is a reference, and A is nothing more than a name, does a class just become part of a lookup table that uses the class name as a key? I am trying to understand what happens to a class at run-time.

4 Answers 4

3

No, A isn't a reference at all. It's just the class name. A is not an expression in its own right - it doesn't have a value. It can only be part of another expression (like A.classVal or new A()).

Sign up to request clarification or add additional context in comments.

2 Comments

but when you call new A(), the jvm must know how to look this up - 'name space = A', call = '()'...
@ThemanontheClaphamomnibus: I suggest you look at the generated byte code - but none of that means that there is "A reference A". How the JVM handles all of this is an implementation detail.
1

First of all,

static refers to the enclosing type and not to the instances

No A does not become a reference to a class Object. Each static member will be created for the type itself and for all the instances of the class the same copy will be used during the execution if you try to access the static member with instance.

It's a way to access the static member in Java with the name of class here A.staticMember.

Comments

1

No. A is a class name, but A.class equals a.getClass().

Comments

1

No, the "A" is just the class. It is not a reference. "a" is an reference object. So, in case you would like to check the class, just compare: a.getClass() vs A.class. They should be equal.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.