This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Java in General and the fly likes curly class field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "curly class field" Watch "curly class field" New topic

curly class field

jack nick
Ranch Hand

Joined: Jan 11, 2001
Posts: 96
hi there,
sorry am making you guys read this big block of code for just a single field. Am talking about the field ErrorCode.class. Very obviously there is a reference made to a static field called class which must resided in the class declaration of ErrorCode or its parents. But it does not seem too.
Problem is why does this code not throw and error. The foreseeable functionality of the said statement is it assigns the ErrorCode class. But how, when and where does it do it from.

public class ErrorCode implements Comparable, Cloneable, {
private java.lang.String _name = "unknown";
public final static Class CLASS = ErrorCode.class;
public final static java.lang.String CLASSNAME = CLASS.getName();
public ErrorCode() {
public int compareTo(Object o) {
ErrorCode data = (ErrorCode) o;
return toString().compareTo(data.toString());
public String toString() {
return _name;
public static void main(String adr[]){
ErrorCode temp = new ErrorCode();
System.out.println("Class name "+ ErrorCode.CLASSNAME);
System.out.println("Class name from method "+ temp.getClass().getName());

Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
ErrorCode.class is a class literal, class is not something declared/defined in the ErrorCode class.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
See Section 15.8.2 of the Java Language Specification on Class Literals.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
...and you may want to take look at The Class class documentation.
Don't get me started about those stupid light bulbs.
subject: curly class field
It's not a secret anymore!