JDK-4957174 : A class level equivalent to "this"
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2003-11-20
  • Updated: 2006-11-09
  • Resolved: 2006-11-09
Related Reports
Relates :  
Relates :  
Description
Name: rmT116609			Date: 11/19/2003


A DESCRIPTION OF THE REQUEST :
While developing static methods within a class, I find that I often need a reference to the class object for that class. I find that I have two alternatives: 1) always use MyClass.class or 2) define a Class constant (I name it thisclass) that is equal to MyClass.class. The drawback to this approach is that if I rename the class, I will need to change this constant. If it is simply a matter of renaming, the compiler will catch the problem. What is usually the cause of my class renames is refactoring, where I rename the old class, create a new base class with the original name, and then distribute the code between the two class appropriately. In this case, I find that forget to update my constant, but this gaff will not be caught at compile time.

JUSTIFICATION :
It simplifies the referencing to the enclosing class within static methods in a manner that removes the maintenance responsibility from the developer and places it into the compiler.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
As with "this", when I refer to the keyword "thisclass", it evaluates to a reference to the owning Class.
ACTUAL -
This behavior is currently not available
(Incident Review ID: 218590) 
======================================================================

Comments
EVALUATION This proposal is similar to the 'this.class' syntax mentioned in 6202132. I can see it's useful to get the Class object for 1) the current object and 2) the class enclosing a static method (though you'd have to rule out MyClass.class in a static initializer). However, I am less interested in obtaining Class objects than I am in a true 'this type' as per 6479372. Once we have such a type, it could be possible to get the Class object with getClass() as usual.
09-11-2006