JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

How To Load A Class Using Class.forName()

April 25, 2014 by Krishna Srinivasan Leave a Comment

This example shows how to load a class using the Class.forName() method. All the classes in your application or dependent class files are loaded by appropriate class loader prior to using in the application. By default, class files are loaded by JVM when first time accessed by the applications. However, in certain situations you have to explicitly load the class files to make it available for your application.

Class.forName() is mostly used for loading the classes. If you would have used the JDBC APIs, there we have loaded the database driver class file using the Class.forName() method. When you pass the class name, note that it has to be passed as the fully qualified path.

Lets look at the below example to understand how the class loader loads the class. If the class is not found by the class loader, JVM will throw java.lang.ClassNotFoundException.

ClassForNameExample.java

[code lang=”java”]
package javabeat.net.core;

/**
* Load class using Class.forName() Example
*
* @author Krishna
*
*/
public class ClassForNameExample {

public static void main(String[] args) {
try {
//Explicitly load class using forName()
Class.forName("javabeat.net.core.One");
} catch (ClassNotFoundException exception){
exception.printStackTrace();
}
try {
//Wrong way of specifying class name. It must be fully qualified path
Class.forName("Two");
} catch (ClassNotFoundException exception){
exception.printStackTrace();
}
try {
//Explicitly load class using forName()
Class.forName("javabeat.net.core.Two");
} catch (ClassNotFoundException exception){
exception.printStackTrace();
}

//Create instance
new Three();
}
}

class One{
static{
System.out.println("Class One Loaded");
}
}

class Two{
static{
System.out.println("Class Two Loaded");
}
}

class Three{
static{
System.out.println("Class Three Loaded");
}
}

[/code]

Output…

[code]
java.lang.ClassNotFoundException: Two
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at javabeat.net.core.ClassForNameExample.main(ClassForNameExample.java:14)
Class One Loaded
Class Two Loaded
Class Three Loaded
[/code]

Filed Under: Java Tagged With: Java Class

How To Get Class Loaded Path

April 24, 2014 by Krishna Srinivasan Leave a Comment

This example illustrates how to get the class loaded path. If you are aware of the java.lang.Class, it has methods for knowing the details of a class. There are many details this object can reveal us, one of the method defined in that class is Class.getProtectionDomain() which is useful for getting the source code path by calling the another method Class.codeSource(). Once we get the CodeSource, getLocation() method returns the path of the class file in the java.net.URL object.

Note that, each instance has “class” variable which returns the java.lang.Class for that particular object.

Lets look this example.

ClassLoadedFromExample.java

[code lang=”java”]
package infosys.net.core;

import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;

/**
* Get class loaded path example
*
* @author Krishna
*
*/
public class ClassLoadedFromExample {
public static void main(String[] args) {
new ClassLoadedFromExample().getClassLocation();
}

/**
* Get the location of the class loaded path.
*/
private void getClassLocation() {
//Get the class object
Class<?> classVar = this.getClass();

//Get the ProtectionDomain
ProtectionDomain protectDomain = classVar.getProtectionDomain();

//Get the CodeSource
CodeSource codeSource = protectDomain.getCodeSource();

//Get location from the CodeSource
URL fileLocation = codeSource.getLocation();

//Print the location of the class
System.out.println("Class Loaded From : " + fileLocation);

}

}
[/code]

Filed Under: Java Tagged With: Java Class

How To Get Implementing Interfaces Name For A Class

April 24, 2014 by Krishna Srinivasan Leave a Comment

This example illustrates how to get the implemented interfaces names of a particular class. If you are aware of the java.lang.Class, it has methods for knowing the details of a class. One of the method defined in that class is Class.getInterfaces() which is useful for getting all the interfaces names which it is implemented. This method returns the array of java.lang.Class instances. With that object array, you can invoke the getName() method to get the name of the class. Note that, each instance has “class” variable which returns the java.lang.Class for that particular object. By using the getName() method, one could get the name of the class.

Lets look this example.

GetImplementingInterfaceExample.java

[code lang=”java”]
package javabeat.net.core;

import java.io.IOException;

/**
* Get Implementing Interface Name Example
*
* @author Krishna
*
*/
public class GetImplementingInterfaceExample {
public static void main(String[] args) throws IOException {
Class<?> classVar[] = null;

int i = 0;

String str = new String();
classVar = str.getClass().getInterfaces();
for (Class cl : classVar) {
System.out.println("String Implemented interface (" + (i + 1) + ")"
+ classVar[i].getName());
i++;
}

Thread thread = new Thread();
classVar = thread.getClass().getInterfaces();
i = 0;
for (Class cl : classVar) {
System.out.println("Thread Implemented interface (" + (i + 1) + ")"
+ classVar[i].getName());
i++;
}
}

}
[/code]

Output…

[code]
String Implemented interface (1)java.io.Serializable
String Implemented interface (2)java.lang.Comparable
String Implemented interface (3)java.lang.CharSequence
Thread Implemented interface (1)java.lang.Runnable
[/code]

Filed Under: Java Tagged With: Java Class

How To Get Super Class Name From Object

April 24, 2014 by Krishna Srinivasan Leave a Comment

This example illustrates how to get the super class name from its instance. If you are aware of the java.lang.Class, it has methods for knowing the details of a class. One of the method defined in that class is Class.getSuperclass() which is useful for getting the super class’s java.lang.Class object. With that object, you can invoke the getName() method to get the name of the class. Note that, each instance has “class” variable which returns the java.lang.Class for that particular object. By using the getName() method, one could get the name of the class.

Lets look this example. This example used few sample classes and getting name of that classes.

GetSuperClassNameExample.java

[code lang=”java”]
package javabeat.net.core;

/**
* Get Super Class Name Example
*
* @author Krishna
*
*/
public class GetSuperClassNameExample {
public static void main(String[] args) {
Class<?> classVar = null;

String str = new String();
classVar = str.getClass().getSuperclass();
System.out.println("String’s Super Class : "+classVar.getName());

Exception e = new Exception();
classVar = e.getClass().getSuperclass();
System.out.println("Exception’s Super Class : "+classVar.getName());

Integer i = new Integer("10");
classVar = i.getClass().getSuperclass();
System.out.println("Integer’s Super Class : "+classVar.getName());

}

}
[/code]

Output…

[code]
String’s Super Class : java.lang.Object
Exception’s Super Class : java.lang.Throwable
Integer’s Super Class : java.lang.Number
[/code]

Filed Under: Java Tagged With: Java Class

How To Get Class Name From Object

April 24, 2014 by Krishna Srinivasan Leave a Comment

This example illustrates how to get the class name from its object. If you are aware of the java.lang.Class, it has methods for knowing the details of a class. One of the method defined in that class is Class.getName() which is useful for getting the name of the class. Note that, each instance has “class” variable which returns the java.lang.Class for that particular object. By using the getName() method, one could get the name of the class.

Lets look this example. This example used few sample classes and getting name of that classes.

GetObjectClassName

[code lang=”java”]
package javabeat.net.core;

/**
* Get Object’s Class Name Example
*
* @author Krishna
*
*/
public class GetObjectClassName {
public static void main(String[] args) {
Class<?> classVar = null;
String str = "Test String";
classVar = str.getClass();
System.out.println("Class Name 1 : " + classVar.getName());

classVar = int.class;
System.out.println("Class Name 2 : " + classVar.getName());

classVar = int[].class;
System.out.println("Class Name 3 : " + classVar.getName());

classVar = Void.TYPE;
System.out.println("Class Name 4 : " + classVar.getName());

classVar = Enum.class;
System.out.println("Class Name 5 : " + classVar.getName());

}
}
[/code]

Output…

[code]
Class Name 1 : java.lang.String
Class Name 2 : int
Class Name 3 : [I
Class Name 4 : void
Class Name 5 : java.lang.Enum
[/code]

Filed Under: Java Tagged With: Java Class

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved