.class and Class.forName

  • Thread starter H.MuthuKumaraRajan
  • Start date
H

H.MuthuKumaraRajan

can any one explain me how .class works and what is the difference
between .class and Class.forName(String).thanks in advance.
Rajan
 
S

Shripathi Kamath

H.MuthuKumaraRajan said:
can any one explain me how .class works and what is the difference
between .class and Class.forName(String).thanks in advance.
Rajan

class is a property of an object of the class Object. It is in a way
similar to the length property for arrays.

Class.forName("YourClass") will work without you having an instance of the
YourClass class. It will return an instance of a Class object that you can
then use to instantiate an object of YourClass. (Using newInstance)

In order to use .class, you will need an instance of the YourClass class.

The rest, and a more accurate description can be had from the JLS or the
javadocs

HTH,
 
A

Andy Fish

as a follow-up question (note I am not the OP), is it possible to get the
current Class from inside a static method?

(without having to do class.forname obviously)

Andy
 
M

Michael Borgwardt

Andy said:
as a follow-up question (note I am not the OP), is it possible to get the
current Class from inside a static method?

In a static method, the "current" class is alywas literally known, so just
use a literal: ClassName.class
 
C

Chris Smith

Shripathi Kamath said:
class is a property of an object of the class Object. It is in a way
similar to the length property for arrays.

Class.forName("YourClass") will work without you having an instance of the
YourClass class. It will return an instance of a Class object that you can
then use to instantiate an object of YourClass. (Using newInstance)

In order to use .class, you will need an instance of the YourClass class.

None of this is correct. See my other reply for details. Perhaps
Shripathi is thinking of the getClass() method, rather than the
'.class' expression.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

H.MuthuKumaraRajan said:
can any one explain me how .class works and what is the difference
between .class and Class.forName(String).thanks in advance.

..class is a "class literal" expression. That is, it's a way to express
the java.lang.Class object for a specific class. On the other hand,
Class.forName can receive any arbitrary String and return a Class object
for the class with that name.

As such, Class.forName may be used to load classes when you don't know
their name at compile-time; they may be plugin classes whose names are
contained in a configuration file somewhere, for example. It can also
be used to reference classes that the compiler doesn't know about, such
as in a case where some libraries aren't available at compile-time.

On the other hand, using a class literal expression such as
'MyClass.class', the compiler is involved in resolving the expression,
and you will get a compile-time error if there's a typographical error
in the class name, for example. This provides the compile-time safety
that makes sense for you to get when you and the compiler both know
about the class you're loading.

So, if it's a matter of obtaining a Class object for a very specific
class, use the class literal expression (MyClass.class). If you need
the flexibility of loading from arbitrary strings with no checks until
runtime, use Class.forName.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
S

Shripathi Kamath

Chris Smith said:
class.

None of this is correct. See my other reply for details. Perhaps
Shripathi is thinking of the getClass() method, rather than the
'.class' expression.

No, Shripathi was plain incorrect on the .class being a property of an
instance, palin and simple. Shripathi confirmed this by looking at his own
usage of .class.

Shripathi notes that some of it (other than the .class part) was indeed
correct.
 
J

Joona I Palaste

No, Shripathi was plain incorrect on the .class being a property of an
instance, palin and simple. Shripathi confirmed this by looking at his own
usage of .class.
Shripathi notes that some of it (other than the .class part) was indeed
correct.

Does Shripathi often speak of himself in the third person?
 
J

John C. Bollinger

Shripathi said:
class is a property of an object of the class Object. It is in a way
similar to the length property for arrays.

Well, sorta.

The .class syntax designates a class literal, and it should be applied
to a class name, not to an object reference (i.e. MyClass.class, not
instanceOfMyClass.class). Compilation of such a literal creates a class
reference in the output class file, and the literal itself resolves to
an object of class Class representing the named class. The existence of
the specified class is checked at compile time, but the class might not
actually be loaded at runtime if no expression involving the class
literal ever needs to be evaluated.

Class.forName(String) is an invocation of a static method of class
Class, which attempts to find, and if necessary load, the named class at
runtime. If successful, it returns an object of class Class
representing the named class. The existence of the named class is not
checked at compile time, but when invoked the method forces the class to
be loaded if it hasn't already been.

The differences are in fact subtle, as you already appreciate if you are
asking the question. As a rule of thumb, the latter is always used if
the name of the class to load is not known at compile time (e.g. a
plug-in), and may sometimes be used in compatability code where the
source makes conditional use of a class that is only available in some
of the Java versions it supports. (A specific example being code that
must support both 1.1-1.3 style focus management and 1.4+ focus
management: the two schemes are not compatible, but you can't compile
with or run on 1.3 if you explicitly use the new 1.4 stuff. You can
work around it with conditional loading of the 1.4 classes, but you
cannot use class literals lest the compilation fail on 1.3.) On the
other hand, where it is possible to get away with it, it is cleaner,
prettier, and just generally better to use the literals.

Examination of the API docs for class Object will show that it has no
field named "class" (although it has an instance method getClass()).
Similarly, array objects do not actually have a field named length.
There is actually a bytecode instruction specifically for returning the
length of an array, and compilation of a construct such as
myArray.length produces bytecode involving that instruction. Thus the
..class and .length syntaxes do indeed have similarities, but not quite
the kind that you described.
Class.forName("YourClass") will work without you having an instance of the
YourClass class. It will return an instance of a Class object that you can
then use to instantiate an object of YourClass. (Using newInstance)
Right.

In order to use .class, you will need an instance of the YourClass class.

Wrong. If you have an object, you can invoke getClass() on it, but you
form a class literal by appending ".class" to the class name, with not
instance required.
The rest, and a more accurate description can be had from the JLS or the
javadocs

And the Java Virtual Machine Specification could be handy, too, but
start with the JLS. (JavaDocs have little to say because they don't
describe array classes and ".class" is not in fact a property of Object.)


John Bollinger
(e-mail address removed)
 
S

Shripathi Kamath

Joona I Palaste said:
Shripathi Kamath <s h r i p a t h i k a m a t h @ h o t m a i l . c o m> scribbled the following:



Does Shripathi often speak of himself in the third person?

Only when Shripathi is being an ass.
 
H

H.MuthuKumaraRajan

Thank you John.

John C. Bollinger said:
Well, sorta.

The .class syntax designates a class literal, and it should be applied
to a class name, not to an object reference (i.e. MyClass.class, not
instanceOfMyClass.class). Compilation of such a literal creates a class
reference in the output class file, and the literal itself resolves to
an object of class Class representing the named class. The existence of
the specified class is checked at compile time, but the class might not
actually be loaded at runtime if no expression involving the class
literal ever needs to be evaluated.

Class.forName(String) is an invocation of a static method of class
Class, which attempts to find, and if necessary load, the named class at
runtime. If successful, it returns an object of class Class
representing the named class. The existence of the named class is not
checked at compile time, but when invoked the method forces the class to
be loaded if it hasn't already been.

The differences are in fact subtle, as you already appreciate if you are
asking the question. As a rule of thumb, the latter is always used if
the name of the class to load is not known at compile time (e.g. a
plug-in), and may sometimes be used in compatability code where the
source makes conditional use of a class that is only available in some
of the Java versions it supports. (A specific example being code that
must support both 1.1-1.3 style focus management and 1.4+ focus
management: the two schemes are not compatible, but you can't compile
with or run on 1.3 if you explicitly use the new 1.4 stuff. You can
work around it with conditional loading of the 1.4 classes, but you
cannot use class literals lest the compilation fail on 1.3.) On the
other hand, where it is possible to get away with it, it is cleaner,
prettier, and just generally better to use the literals.

Examination of the API docs for class Object will show that it has no
field named "class" (although it has an instance method getClass()).
Similarly, array objects do not actually have a field named length.
There is actually a bytecode instruction specifically for returning the
length of an array, and compilation of a construct such as
myArray.length produces bytecode involving that instruction. Thus the
.class and .length syntaxes do indeed have similarities, but not quite
the kind that you described.


Wrong. If you have an object, you can invoke getClass() on it, but you
form a class literal by appending ".class" to the class name, with not
instance required.


And the Java Virtual Machine Specification could be handy, too, but
start with the JLS. (JavaDocs have little to say because they don't
describe array classes and ".class" is not in fact a property of Object.)


John Bollinger
(e-mail address removed)
 
H

H.MuthuKumaraRajan

Thank you everyone
Chris Smith said:
.class is a "class literal" expression. That is, it's a way to express
the java.lang.Class object for a specific class. On the other hand,
Class.forName can receive any arbitrary String and return a Class object
for the class with that name.

As such, Class.forName may be used to load classes when you don't know
their name at compile-time; they may be plugin classes whose names are
contained in a configuration file somewhere, for example. It can also
be used to reference classes that the compiler doesn't know about, such
as in a case where some libraries aren't available at compile-time.

On the other hand, using a class literal expression such as
'MyClass.class', the compiler is involved in resolving the expression,
and you will get a compile-time error if there's a typographical error
in the class name, for example. This provides the compile-time safety
that makes sense for you to get when you and the compiler both know
about the class you're loading.

So, if it's a matter of obtaining a Class object for a very specific
class, use the class literal expression (MyClass.class). If you need
the flexibility of loading from arbitrary strings with no checks until
runtime, use Class.forName.
 

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

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top