Class names don't identify a class - Java Tutorials

class names dont identify class

Class names don't identify a class - Java Tutorials

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.

Tutorials

  1. Assertion in java
  2. Anonymous Inner Classes - Anonymous Inner Classes tutorial
  3. Appending Strings - Java Tutorials
  4. Assertion in Java
  5. Autoboxing unboxing in Java - Java Tutorials
  6. Thread Deadlocks - Java Tutorials
  7. BASIC Java - Java Tutorials
  8. Interthread Communication in Java
  9. boolean comparisons - tutorial
  10. Catching Exceptions in GUI Code - Java Tutorials
  11. Exception in Java - Java Tutorials
  12. Causing Deadlocks in Swing Code
  13. Class names don't identify a class - Java Tutorials
  14. Commenting out your code - Java Tutorials
  15. Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java
  16. Disassembling Java Classes - Java Tutorials
  17. Double-checked locking,java tutorials,java tutorial
  18. Exceptional Constructors - Java Tutorials
  19. Final Methods - Java Tutorials
  20. garbage collection in java
  21. Java - JDK Tutorials
  22. J2EE Singleton Pattern - Design Pattern Tutorials
  23. Java Comments - Java Tutorials
  24. Java Field Initialisation - Java Tutorials
  25. Java HashSet  - Java Tutorials
  26. Java Multi Dimensions Array - Java Tutorials
  27. java awt package tutorial
  28. Java GC
  29. Java HashMap - Java Tutorials
  30. JDK 1.4 the NullPointerException - Java Tutorials
  31. HashMap and HashCode
  32. LinkedHashMap - Java Tutorials
  33. Which is Faster - LinkedList or ArrayList?
  34. Making Enumerations Iterable - JDK 5 Example
  35. Making Exceptions Unchecked - java tutorial,java tutorials
  36. Creation Time Comparison of Multi Dimensional Array- Java Tutorials
  37. Multicasting in Java - java tutorials,tutorial
  38. Non-virtual Methods in Java - java tutorials
  39. Orientating Components Right to Left,java newsletter,java,tutorial
  40. The link to the outer class,java tutorial,java tutorials