Mike
Mike

Reputation: 60812

Java terminology for differentiating runtime type from compile-time type

In Java, an Object can have a runtime type (which is what it was created as) and a casted type (the type you have casted it to be).

I'm wondering what are the proper name for these types. For instance

class A {

}

class B extends A {

}

A a = new B();

a was created as a B however it was declared as an A. What is the proper way of referring to the type of a using each perspective?

Upvotes: 1

Views: 1762

Answers (8)

Jon Skeet
Jon Skeet

Reputation: 1503090

I think it's important to distinguish between the object (which exists at execution time, and just has its execution time type) and an expression (such as a variable) which has a compile-time type.

So in this case:

A a = new B();

a is a variable, of type A. Its value at execution time is a reference to an object of type B.

The Java language specification uses "run-time class" (e.g. for the purpose of overriding, as in section 15.12.4.4) for the type of an object. Elsewhere I think it just uses "type" for the type of an expression, meaning the compile-time type.

Upvotes: 10

Mario Galic
Mario Galic

Reputation: 48430

Section 15.5. Expressions and Run-Time Checks differentiates between

  • the type of an expression
  • the class of the referenced object

For example,

If the type of an expression is a reference type, then the class of the referenced object, or even whether the value is a reference to an object rather than null, is not necessarily known at compile time. There are a few places in the Java programming language where the actual class of a referenced object affects program execution in a manner that cannot be deduced from the type of the expression...

[snip]

An expression whose type is a reference type may be tested using instanceof to find out whether the class of the object referenced by the run-time value of the expression

Hence, applying the above language to

A a = new B();

we might say something like

The static type of the expression a is A, despite the fact the value stored in a is a reference to the object of runtime class B.

Personally, I interpret the two concepts in the following manner (but beware I am unsure of its correctness):

  • static type of the expression is purely syntactic concept existing in source code at compile time for processing by Java compiler
  • runtime class of the object is actual machine construct existing in machine memory at runtime for processing by Java Virtual Machine

Upvotes: 0

sql_dummy
sql_dummy

Reputation: 745

To determine a is object of which class you can use:

/*The java.lang.Object.getClass() method returns the runtime class of an object*/   
System.out.println("a is object of: "+a.getClass());

Upvotes: 0

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103145

The terminology you are looking for is the Apparent Type and the Actual Type.

A a = new B();

The Apparent Type is A because the compiler only knows that the object is of type A. As such at this time you cannot reference any of the B specific methods.

The Actual Type is B. You are allowed to cast the object (that is change its apparent type) in order to access the B specific methods.

Upvotes: 0

meriton
meriton

Reputation: 70584

The Java Language Specification speaks about a variable's declared type, the javadoc of getClass() about an object's runtime class.

Note that there is no such thing as a runtime type in Java; List<String> and List<Integer> are different types, but their instances share the same runtime class.

Upvotes: 5

levik
levik

Reputation: 117589

In this case, A is the reference type while B is the instance type

Upvotes: 2

Bill the Lizard
Bill the Lizard

Reputation: 405995

The type of the variable a is A. There's no changing that, since it's a reference. It happens to refer to an object of type B. While you're referring to that B object through an A reference you can only treat it as though it were of type A.

You can later cast it to its more specific type

B b = (B)a;

and use the B methods on that object.

Upvotes: 1

Mikael Auno
Mikael Auno

Reputation: 9060

I would say that you differentiate between the type of the variable/reference and the type of the object. In the case

A a = new B();

the variable/reference would be of type A but the object of type B.

Upvotes: 1

Related Questions