Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

curly class field

 
jack nick
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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, java.io.Serializable {
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());
}
}

Cheers
Jack
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ErrorCode.class is a class literal, class is not something declared/defined in the ErrorCode class.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Section 15.8.2 of the Java Language Specification on Class Literals.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and you may want to take look at The Class class documentation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic