class/getClass()

D

Dennis

In class Object, getClass() returns a Class object representing
whatever object the method was called on. In something like:

initialize(UserContext.class);

protected void initialize (Class cls){
initializeAtributes(cls);
initializeDomains(cls);
}

something similar appears to be happening with _class_. What is this
and where is _class_ defined? It looks like UserContext, or something
it's extending, has an attribute named _class_, but I don't see it
anywhere. Also, MyEclipse highlights this along with other java
keywords. I've poked around the language specification but couldn't
find anything about it.

Someone embarrass me and tell me where it is in the spec. Please.

Thanks.
 
A

Arne Vajhøj

Dennis said:
In class Object, getClass() returns a Class object representing
whatever object the method was called on. In something like:

initialize(UserContext.class);

protected void initialize (Class cls){
initializeAtributes(cls);
initializeDomains(cls);
}

something similar appears to be happening with _class_. What is this
and where is _class_ defined? It looks like UserContext, or something
it's extending, has an attribute named _class_, but I don't see it
anywhere. Also, MyEclipse highlights this along with other java
keywords. I've poked around the language specification but couldn't
find anything about it.

Someone embarrass me and tell me where it is in the spec. Please.

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#251530

Arne
 
J

Joshua Cranmer

Dennis said:
In class Object, getClass() returns a Class object representing
whatever object the method was called on. In something like:

initialize(UserContext.class);

protected void initialize (Class cls){
initializeAtributes(cls);
initializeDomains(cls);
}

something similar appears to be happening with _class_. What is this
and where is _class_ defined? It looks like UserContext, or something
it's extending, has an attribute named _class_, but I don't see it
anywhere. Also, MyEclipse highlights this along with other java
keywords. I've poked around the language specification but couldn't
find anything about it.

The `class' keyword, when used like a static variable, returns an object
of type Class<T> (where T is the type name to which it is attached) [1].
Typically, Class objects are used for reflection purposes, although they
are occasionally used for other purposes.

[1] There is a slight bug in the Java compiler whereby List.class
returns Class<List> and not Class<List<?>> (List<?>.class does not
work); this has previously caused at least me some enormous grief.
 
D

Daniel Pitts

Joshua said:
Dennis said:
In class Object, getClass() returns a Class object representing
whatever object the method was called on. In something like:

initialize(UserContext.class);

protected void initialize (Class cls){
initializeAtributes(cls);
initializeDomains(cls);
}

something similar appears to be happening with _class_. What is this
and where is _class_ defined? It looks like UserContext, or something
it's extending, has an attribute named _class_, but I don't see it
anywhere. Also, MyEclipse highlights this along with other java
keywords. I've poked around the language specification but couldn't
find anything about it.

The `class' keyword, when used like a static variable, returns an object
of type Class<T> (where T is the type name to which it is attached) [1].
Typically, Class objects are used for reflection purposes, although they
are occasionally used for other purposes.

[1] There is a slight bug in the Java compiler whereby List.class
returns Class<List> and not Class<List<?>> (List<?>.class does not
work); this has previously caused at least me some enormous grief.
I don't know if its a bug as much as it is a missing feature. Since
Java currently uses Type erasure, there are a lot of things it can't do...

Think about the type that would be List<Class<? extends
Foo>>.class.getClass(); Class<Class<List<Class<? extends Foo>>>>.
Whats worse is that value would .equals any other class instance. Once
(if) they make generics reifiable, the problem goes away.
 
M

Mark Space

Dennis said:
In class Object, getClass() returns a Class object representing
whatever object the method was called on. In something like:

initialize(UserContext.class);

protected void initialize (Class cls){
initializeAtributes(cls);
initializeDomains(cls);
}

something similar appears to be happening with _class_. What is this
and where is _class_ defined? It looks like UserContext, or something
it's extending, has an attribute named _class_, but I don't see it

What others have said. ".class" is a psuedo field that the compiler
maintains for you. It's a bit like the ".this" keyword, or .length for
arrays. Classname.class gives the Class type. It's a literal.
object.getClass() is an actual function call that burns CPU cycles at
runtime. ".class" doesn't, it's resolved at compile time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top