reflection and CLASSPATH

L

learningjava

Hi,

I have read somewhere on this list a suggestion that
if you want to use reflection in a program, either the classpath should contain
the current program's path OR install a ClassLoader.
I have no idea what a ClassLoader does, but can someone enlighten me on that?

Thanks in advance,

a beginner
 
L

Lee Fesperman

learningjava said:
Hi,

I have read somewhere on this list a suggestion that
if you want to use reflection in a program, either the classpath should contain
the current program's path OR install a ClassLoader.

You can safely ignore that advice. Reflection doesn't have any more special requirements
on the classpath or ClassLoaders than a normal application. It all depends on what you
are trying to do.
I have no idea what a ClassLoader does, but can someone enlighten me on that?

A ClassLoader loads classes. That is, it obtains the bytecode representation of a Class,
loads it into the JVM and makes it available to the program. The JVM provides a default
ClassLoader. You only need to install another ClassLoader if you have specific
requirements.
 
L

learningjava

Thanks, Lee.

But I'm not able to get it work for me.

Then why is Class.forName() unable to find a class (an inner class of
the same program)?Gives a ClassNotFoundException.

Gauri
 
C

Chris Uppal

learningjava said:
Then why is Class.forName() unable to find a class (an inner class of
the same program)?Gives a ClassNotFoundException.

Perhaps because you are looking for the wrong name. This example works:

========= Test.java ===========

public class Test
{
static class Nested
{
}

public static void
main(String[] args)
throws ClassNotFoundException
{
Class thisClass = Class.forName("Test");
Class nestedClass = Class.forName("Test$Nested"); // <<-- HERE

System.out.println("This class is " + thisClass);
System.out.println("Nested class is " + nestedClass);
}
}

========= Test.java ===========

The '$' is because there is no such thing as a nested class -- the compiler
fakes them using "real" classes with '$'s in their names. If you list the
generated .class files you'll see what the real names are.

-- chris
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top