0

Every object in Java belongs to a certain class. That's why the Object class, which is inherited by all other classes, defines the getClass().

getClass() method returns the instance of Class class.

For example:

class Foo{}
class Sample{ class Foo instance = Foo(); Class obj = instance.getClass(); }

Another way of getting the instance of Class class is by saying Foo.class

My question:

I can see the definition of getClass() in Object class source code(File Object.java package java.lang). In which source file, can i see the member class which we are using as Foo.class?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • 1
    possible duplicate of [Difference between a.getClass() and A.class?](http://stackoverflow.com/questions/10947768/difference-between-a-getclass-and-a-class) – Mandar Pandit Jun 20 '14 at 06:32
  • This might be already answered.. (http://stackoverflow.com/questions/10947768/difference-between-a-getclass-and-a-class) – Mandar Pandit Jun 20 '14 at 06:34
  • 1
    Check the other answers, and also [this from the JLS](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.8.2). the `.class` is an expression called a class literal. – Deco Jun 20 '14 at 06:38
  • @Deco So this is a syntactic stuff as per the given link of JLS. Deco, if this duplicate question? – overexchange Jun 20 '14 at 06:41
  • @overexchange - not really. `.getClass()` and `.class` are used for different things. The former is used at run time, the latter is checked at compile-time. Check out the linked question and its answers. – Deco Jun 20 '14 at 06:44
  • @Deco i think i already know the difference in getting runtime/compile time type. I think, my question is not referring to this point. Can you please go thru my question again? – overexchange Jun 20 '14 at 06:46
  • @Deco I did not understand the meaning of these statements from the JLS link. Can you give an example? "It is a compile-time error if the named type is a type variable (§4.4) or a parameterized type (§4.5) or an array whose element type is a type variable or parameterized type. It is a compile-time error if the named type does not denote a type that is accessible (§6.6) and in scope (§6.3) at the point where the class literal appears." – overexchange Jun 20 '14 at 07:04
  • The named type cannot be a type variable (e.g. `T` if you have a parameterized class such as `SomeClass`), parameterized type (e.g. can't have `ArrayList.class`, only can have `ArrayList.class`), or an array whose element is a type variable (e.g. can't have `T[].class` in a class `SomeClass`) or parameterized type (e.g. can't have `ArrayList[].class`). Last two points just mean that these literals are subject to the same access/scoping rules (i.e. `private`, `protected`, etc.) that other variables are. – awksp Jun 20 '14 at 10:54
  • @user3580294 From usage perspective, we can use getClass() method in finding some third party class meta info. for example if 'Object obj' is the parameter of my method, i can get the actual type of obj by saying 'obj.getClass()', But i did not understand the necessity of Foo.class in real time. – overexchange Jun 21 '14 at 09:38
  • You have to use a class literal in `instanceof` expressions, for example, as the compiler requires that the right side of the expression be a compile-time constant. It could be useful other instances too if you happen to already know the class you want, such as in the parameter for a method. – awksp Jun 21 '14 at 09:40
  • @user3580294 One last point, I did not understand this statement from Java Doc, "The primitive Java types ({@code boolean}, {@code byte}, {@code char}, {@code short}, {@code int}, {@code long}, {@code float}, and {@code double}), and the keyword {@code void} are also represented as {@code Class} objects." – overexchange Jun 21 '14 at 09:47
  • `boolean.class`, `byte.class`, etc. are all valid class literals. To be honest, I haven't run into a case where those would be used, but I suspect that they would find a use for reflection (which I've been lucky enough to not need to use). – awksp Jun 21 '14 at 09:51
  • @user3580294 In statement, 'char ch='A';' 'A' is character literal, But i did not understand the meaning of class literal, when you say for example, boolean.class or Foo.class is class literal – overexchange Jun 21 '14 at 10:02
  • A "class literal" is defined in the JLS to be "an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type void, followed by a '.' and the token class." So `String` is a class, `Class` is the class object for `String`, and `String.class` is the class literal for `Class`. – awksp Jun 21 '14 at 10:04

2 Answers2

1

No source file. class is a keyword (like this, instanceof, etc), not a member of any class, and is handled by the compiler, not in code.

Alvin Thompson
  • 5,388
  • 3
  • 26
  • 39
0

If an instance of an object is available, then the simplest way to get its Class is to invoke Object.getClass()

Foo foo=new Foo();
Class c = foo.getClass();

If the type is available but there is no instance then it is possible to obtain a Class by appending .class to the name of the type. This is also the easiest way to obtain the Class for a primitive type.

boolean b;
Class c = boolean.class; 

and this would produce compile-time error

Class c = b.getClass();   

because a boolean is a primitive type and cannot be dereferenced

And for something like this

Foo foo=null;

note that you cannot use foo.getClass() in this case since it is not instantiated.

And finally for something like this

Foo foo=new FooChild();
Class c= foo.getClass(); //returns FooChild, evaluates at runtime
Class c= Foo.class;// returns Foo , evaluates statistically at compile-time.

Edit:- For .class its a static field inside every primitive type, static Class<Integer> ,The Class instance representing the Integer. you can see it here for Integer , here for Boolean. boolean, byte, char, short, int, long, float, and double all of them have a Class static field because like i said its always going to stay same and primitive types cannot be cannot be dereferenced. To see the source, if you see source of Integer you can see the class field as public static final Class<Integer> TYPE = (Class<Integer>) VMClassLoader.getPrimitiveClass('I'); , see here line 82. You can search and see for others too.

Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28
  • foo.class; This is wrong. BTW the links that Mandar is pointing to already answers this, but my question is something else, I did not ask when to use getClass() and when to use class literal? – overexchange Jun 20 '14 at 06:48
  • I have edited, meant Foo.class . Also updated answer to include answer to `, can i see the member class which we are using as Foo.class` – Mustafa sabir Jun 20 '14 at 07:13