Mario Dennis
Mario Dennis

Reputation: 3016

Working with the class keyword in Java

I don't really understand how the class keywords work in some instances.

For example, the get(ClientResponse.class) method takes the ClientResponse.class. How does it use this when it gets it, and what are the advantages over just passing an instance of it?

Upvotes: 12

Views: 11363

Answers (8)

Ashutosh
Ashutosh

Reputation: 11

Whenever we compile any Java file, the compiler will embed a public, static, final field named class, of the type java.lang.Class, in the emitted byte code. Since this field is public and static, we can access it using dotted notation along with class name as in your case it is ClientResponse.class.

Upvotes: 0

Krzysztof Jabłoński
Krzysztof Jabłoński

Reputation: 1941

The very most important fact is - you don't need to have an instance to call the method. It's critically useful in situations when you cannot for some reason instantiate a class, e.g. it's abstract, or have only private constructor, or can only be correctly instantiated by some framework, like Spring or JSF.

You can then call get to obtain an object of a requested type without even knowing where it does come from and how it get's created.

Upvotes: 2

Chip
Chip

Reputation: 3316

A class is a "blueprint" of the object. The instance is a object.

If we have

public class SomeClass {
   int a;
   SomeClass(int a) {
      this.a = a
   }
}

We can have an instance of this class

SomeClass c = new SomeClass(10);

c is an instance of the class. It has a integer a with value 10.

The object SomeClass.class represents a Class.

Here SomeClass.class is a object of the type Class which has the information that SomeClass is

  1. a concrete class with
  2. one constructor
  3. with a integer member variable

    and lots more other metadata about the class SomeClass. Note that it does not have a value for a.

You should use get(c) incase you are planning to do something with a instance of c like call c.a or other useful functions to manupulate/get data of the instance.

You should use get(SomeClass.class) when the get returns something based on the fact that the argument is some type of class. For example, if this is a method on a Registry class which has a map which retrieves a implementation class based on type of class passed in.

Upvotes: 2

duffymo
duffymo

Reputation: 308998

The Class class, which is different from the class keyword, is meta-data describing instances. It tells you about the methods, data members, constructors, and other features of the instances that you create by calling new.

For example get(ClientResponse.class) method takes the ClientResponse.class how does it uses this when it gets it and what are the advantage over just passing a instance of it?

You can't pass an instance of ClientResponse to this method; it's expecting meta-data about all instances of ClientResponse. If you passed an instance, you'd expect that the method might change the state of that instance. But passing the meta-data about all instances might allow the method to create a new kind of instance (e.g. a dynamic proxy) or do something else that depends on the meta-data about all instances of ClientResponse. See the difference?

Upvotes: 2

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

In ClientResponse.class, class is not a keyword, neither a static field in the class ClientResponse.

The keyword is the one that we use to define a class in Java. e.g.

public class MyClass { } /* class used here is one of the keywords in Java */

The class in ClientResponse.class is a short-cut to the instance of Class<T> that represents the class ClientResponse.

There is another way to get to that instance for which you need an instance of ClientResponse. e.g

ClientResponse obj = new ClientResponse();
Class clazz = obj.getClass(); 

what are the advantage over just passing a instance of it?

In the above example you can see what would happen in case obj was null (an NPE). Then there would be no way for the method to get the reference to the Class instance for ClientResponse.

Upvotes: 7

Kurtymckurt
Kurtymckurt

Reputation: 337

There's a lot of ways Class objects can be used. This is used for Reflection. Below is a link that can help you understand more.

http://docs.oracle.com/javase/tutorial/reflect/class/classNew.html

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340933

Here ClientResponse.class is an instance of Class<ClientResponse>. In general Class object represents type of an object. When you create new instance:

Object obj = new ClientResponse()

you can retrieve the class (type) of that object by calling:

obj.getClass()

So, why would you pass Class objects around? It's less common, but one reason is to allow some method create arbitrary number of instances of a given class:

ClientResponse resp = ClientResponse.newInstance();

Upvotes: 1

smcg
smcg

Reputation: 3273

SomeClass.class

returns a Java Class object. Class is genericized, so the actual type of SomeClass.class will be Class<SomeType> .

There are lots of uses for this object, and you can read the Javadoc for it here: http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html

Upvotes: 10

Related Questions