Does this Java snippet seem needlessly redundant?

Status
Not open for further replies.

jbode

Ars Legatus Legionis
11,752
Subscriptor
I was poking around in the codebase I just started working with, and found an interface with the following definitions:
Code:
public static final Class INTEGER_CLASS = (new Integer(0)).getClass();
public static final Class DOUBLE_CLASS = (new Double(0)).getClass();
// etc.

Two questions popped to mind: first, since I know this code postdates 1.1, why not just use Integer.class, Double.class, etc.? Second, why define those symbolic constants at all if we can just use Integer.class, Double.class, etc.?
 

agifford

Ars Tribunus Militum
2,142
.class Isn't in the api docs (to my knowledge), its a keyword or something which is documented elsewhere. So the author may have been unaware of it, but saw .getClass();

defining a constant and then using the constant elsewhere would make it easier to change which class is used for a set of items, instead of having to determine if each place was using it for the same reason.

That and, if you don't know about .class, you dont want to have to write (new Double(0)).getClass(), a bunch of times ;)
 
The extra parenthesis around new Integer(0) and new Double(0) give away the answer. Calling getClass() on parentheses returns a parenthetical class (kind of a placeholder to wrap other classes). If the thing inside the parentheses is also a class, it becomes an inner class. Java has supported inner classes since version 1.1. Furthermore, when the 0 in the constructor gets unboxed you have an Integer class inside an Integer class inside a parenthetical class, which is what getClass() in this case actually returns. Unfortunately, AFAIK this is the only way to get ahold of such an exotic class, and in fact, this is one of the few entirely undocumented and most powerful features of Java.

P.S. When creating double nested classes, it's important to remember to put them at the top of the source file (the lines immediately after "class MyClass {"), so that the Java preprocessor sees them and can put them in the class header files. Always run the preprocessor before you compile so that the header files match the class files (otherwise you get weird errors at runtime).
 

Redshift

Ars Scholae Palatinae
1,260
Personally I find this construct and constant really useful.

Code:
	public static final Class INTEGER_CLASS = (((((((((((((new Integer(0)))))))))))))).getClass();


I've been asking for this syntax to be added to the language.


Code:
	public static final Class INTEGER_CLASS1 = (^10 new Integer(0) )^10.getClass();

But so far my parentheses to the 10th power recommendation has been rejected.
 
Redshift":2snugpsh said:
Personally I find this construct and constant really useful.

Code:
	public static final Class INTEGER_CLASS = (((((((((((((new Integer(0)))))))))))))).getClass();


I've been asking for this syntax to be added to the language.


Code:
	public static final Class INTEGER_CLASS1 = (^10 new Integer(0) )^10.getClass();

But so far my parentheses to the 10th power recommendation has been rejected.
:rolleyes: Obviously, your proposal was rejected because it makes no sense to say (^10 to denote 13 (s.
 
Status
Not open for further replies.