Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange class declaration

 
Panseer Kaur
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I've been reading the forum for a while trying to learn Java. More recently I thought it would be a good idea to try to read the Java source code to the libraries. This has been pretty helpful but then I came across this line in Integer.java

public static final Class TYPE = Class.getPrimitiveClass("int");

This really confused me because I thought if we are defining a class it had to be:

public static final Class TYPE {

TYPE () {

return Class.getPrimitiveClass("int");

}

I know there are anonymous classes, is it possible this is what this is?

Thanks ahead of time
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15437
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

public static final Class TYPE = Class.getPrimitiveClass("int");

Note that it says "Class" with a capital C, not "class" with a lower-case c.

The word "class" with a lower-case c is a Java keyword that is used to declare new classes, but the word "Class" with a capital C is something completely different: it's the class named "Class" in the java.lang package.

So, TYPE is an instance of class Class in the line of code above. It's not a class declaration at all. Do you see?

Look up the API documentation of class java.lang.Class.
[ November 01, 2007: Message edited by: Jesper Young ]
 
Panseer Kaur
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup I get it. Makes perfect sense now.

Should have been obvious to me. Thanks though
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic