JDK-6202132 : Add the syntax myObject.class to retrieve the Class of an object
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-11-30
  • Updated: 2010-04-04
  • Resolved: 2006-11-09
Related Reports
Relates :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
I can't for the life of me see why .class is allowed after a class name but not a reference. Instead you have to type the incantation myObject.getClass().

Here's an example I just came across:

public boolean equals(Object other) {	if (other.getClass() == Bananas.class) { // check it's not a subclass		// ...blah...}

How much clearer and more consistent if we could write:

if (other.class == Bananas.class) { // check it's not a subclass

In effect Object would be acknowledged as having the following field:
public final Class<? extends Object> class;

...just as arrays have the following field:

public final int length;

In fact the above code could be made even clearer and more portable, since you wouldn't have to name the current class (Bananas) directly:

if (other.class == this.class) { // check it's not a subclass


JUSTIFICATION :
This would be more consistent with the use of .class after a class name. It is not clear why the 'static' use of .class is allowed but not the 'non-static'.

It would also acknowledge, in the language, the existence of a 'field' which all moderately advanced developers are already aware of. Anybody, for instance, who uses reflection knows that an object has access to its own class. (Maybe anybody who uses 'virtual' methods or instanceof.)

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
myRef.class should be equivalent to myRef.getClass() for any reference myRef.
this.class should also be allowed - see above example.
Possibly super.class could be supported, equivalent to this.class.getSuperclass(), but there might be arguments against it.

I am explicitly not proposing that java.lang.Class.getFields() simulates the existence of this class 'field'.

CUSTOMER SUBMITTED WORKAROUND :
Use Object.getClass().

Comments
EVALUATION I see this CR is a "See Also" on 6479372. I want to make clear that 'this.class' as proposed here (6202132) is about the value of an existing Class object, whereas 6479372 (and any other ThisClass proposal) is about an entirely new type. (Of course, if we did accept 6202132 and 6479372, the expression this.class would return an object of type ThisClass.)
09-11-2006

EVALUATION There is an argument that since Banana.class is shorthand for Class.forName("Banana"), we should allow x.class as shorthand for x.getClass() when x is a variable. However, Banana.class has the static type Class<Banana> which is an improvement over the less precise type Class<?> returned by Class.forName. There is no such win with the proposal here: other.class is just syntactic sugar so continues to have the static type Class<? extends Object>. As such, the syntax isn't a big win, and starts us down a road of other.superclass, etc, when the reflection API gives you what you need. Furthermore, what do we do if 'other' is both a class and a variable? (Currently, an 'other' variable obscures an 'other' class in most places *except* in class literals.) A more useful addition to reflection would be a true 'this' type.
09-11-2006