Home Javatutorials Class names don't identify a class - Java Tutorials
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Class names don't identify a class - Java Tutorials

Advertisement
class names dont identify class

Class names don't identify a class

In JDK 1.2 the Sum Microsystems have added a new feature that allows you to identify a class not only by its name but also by its context for which it was loaded. For this you need to set the ClassLoader for a thread, which then loaded the classes. In JDK 1.2 or above version there is a new class  URLClassLoader, you can point a directory to this class loader, then it loaded the classes from their. Consider a simple example given below,

Consider the FirstClass class saved in directory1 given below

public class FirstClass{
	public String toString(){
		return "This is a First Class";
	}
}

And another first class in directory2

public class FirstClass{
	public String toString(){
		return "This is a Second Class";
	}
}

As you can see the two classes is completely different but there method declaration is different. The general way to use this class at run time instead of compile time. Consider the Simple Test class given below.

Test.java

public class Test {
	public static void main(String[] args) {
		System.out.println(new FirstClass());
	}
}

Compile the Test class as

C:\>javac -cp .;directory1 Test.java

Now run this program as

C:\>java -cp .;directory1 Test
This is a First Class

But suppose a situation where you wish to use the make an instance of both the classes at the same time. Generally you can not do that but with the help of ClassLoader you can do that. Consider a simple class which uses ClassLoader to load the different class instances at the same time.

SimpleClassLoader.java

import java.net.*;

public class SimpleClassLoader {
	public static void main(String[] args) throws Exception {

		// Loading First Directory
		ClassLoader classLoader1 = new URLClassLoader(new URL[] { new URL(
				"file:directory1/") }, null);
		// Loading Second Directory
		ClassLoader classLoader2 = new URLClassLoader(new URL[] { new URL(
				"file:directory2/") }, null);

		Class firstClass = classLoader1.loadClass("FirstClass");
		Class secondClass = classLoader2.loadClass("FirstClass");
		System.out.println("fisrtClass Called - " + firstClass);
		System.out.println("secondClass Called - " + secondClass);
		System.out.println("Comparing Calsses - "
				+ firstClass.equals(secondClass));
		System.out.println("Running Fisrt Class - " + firstClass.newInstance());
		System.out.println("Running Second Class - "
				+ secondClass.newInstance());
	}
}

Now When you run this class you will see both the instance of the FirstClass with the different directory will run at the same time, The output of the above class is as,

C:\>java -cp .;directory2 SimpleClassLoader
fisrtClass Called - class FirstClass
secondClass Called - class FirstClass
Comparing Calsses - false
Making Fisrt Class Instance - This is a First Class
Making Second Class Instance - This is a Second Class

So the above explanation show that the class name is not enough to identify a class name.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Related Tutorials

Ask Questions?    Discuss: Class names don't identify a class - Java Tutorials  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com