Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

Class names don't identify a class - Java Tutorials

class names dont identify class


Java final method Assert in Java Final method in java Autoboxing in Java assert In Java JDBC batch insert using Java bean class Java Singleton Pattern Java Collection API - Java Tutorials Stack Overflow - Java Tutorials End of Year Puzzle The link to the outer class,java tutorial,java tutorials Orientating Components Right to Left,java newsletter,java,tutorial Non-virtual Methods in Java - java tutorials Multicasting in Java - java tutorials,tutorial Creation Time Comparison of Multi Dimensional Array- Java Tutorials Making Exceptions Unchecked - java tutorial,java tutorials Making Enumerations Iterable - JDK 5 Example Which is Faster - LinkedList or ArrayList? LinkedHashMap - Java Tutorials HashMap and HashCode JDK 1.4 the NullPointerException - Java Tutorials Java HashMap - Java Tutorials Java GC java awt package tutorial Java Multi Dimensions Array - Java Tutorials Java HashSet  - Java Tutorials Java Field Initialisation - Java Tutorials Java Comments - Java Tutorials J2EE Singleton Pattern - Design Pattern Tutorials Java - JDK Tutorials garbage collection in java Final Methods - Java Tutorials Exceptional Constructors - Java Tutorials Double-checked locking,java tutorials,java tutorial Disassembling Java Classes - Java Tutorials Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java Commenting out your code - Java Tutorials Class names don't identify a class - Java Tutorials Causing Deadlocks in Swing Code Exception in Java - Java Tutorials Catching Exceptions in GUI Code - Java Tutorials boolean comparisons - tutorial Interthread Communication in Java BASIC Java - Java Tutorials Thread Deadlocks - Java Tutorials Autoboxing unboxing in Java - Java Tutorials Assertion in Java Appending Strings - Java Tutorials Anonymous Inner Classes - Anonymous Inner Classes tutorial Assertion in java

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.

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

Compile the Test class as

C:\>javac -cp .;directory1

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.


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.


Share on Google+Share on Google+

Class names don't identify a class - Java Tutorials

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles



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

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image