The Wayback Machine - https://web.archive.org/web/20240819055647/https://www.geeksforgeeks.org/class-forname-method-in-java-with-examples/
Open In App

Class forName() method in Java with Examples

Last Updated : 01 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The forName() method of java.lang.Class class is used to get the instance of this Class with the specified class name. This class name is specified as the string parameter.
Syntax: 
 

public static Class<T> forName(String className) throws ClassNotFoundException

Parameter: This method accepts the parameter className which is the Class for which its instance is required.
Return Value: This method returns the instance of this Class with the specified class name.
Exception: This method throws following Exceptions: 
 

  • LinkageError: if the linkage fails
  • ExceptionInInitializerError: if the initialization provoked by this method fails
  • ClassNotFoundException: if the class cannot be located

Below programs demonstrate the forName() method.
Example 1:
 

Java




// Java program to demonstrate forName() method
 
public class Test {
    public static void main(String[] args)
        throws ClassNotFoundException
    {
        // get the Class instance using forName method
        Class c1 = Class.forName("java.lang.String");
 
        System.out.print("Class represented by c1: "
                         + c1.toString());
    }
}


Output: 

Class represented by c1: class java.lang.String

 

Example 2:
 

Java




// Java program to demonstrate forName() method
 
public class Test {
    public static void main(String[] args)
        throws ClassNotFoundException
    {
        // get the Class instance using forName method
        Class c1 = Class.forName("java.lang.Integer");
 
        System.out.print("Class represented by c1: "
                         + c1.toString());
    }
}


Output: 

Class represented by c1: class java.lang.Integer

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#forName-java.lang.String-
 



Similar Reads

Class forName(String, boolean, ClassLoader) method in Java with Examples
The forName(String, boolean, ClassLoader) method of java.lang.Class class is used to get the instance of this Class with the specified class name, using the specified class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.Syntax: public static Class&lt;T&gt; forName(String classNam
2 min read
Charset forName() method in Java with Examples
The forName() method is a built-in method of the java.nio.charset returns a charset object for the named charset. In this function we pass a canonical name or an alias and its respective charset name is returned. Syntax: public static Charset forName?(String charsetName) Parameters: The function accepts a single mandatory parameter charsetName whic
3 min read
Java.util.concurrent.RecursiveAction class in Java with Examples
RecursiveAction is an abstract class encapsulates a task that does not return a result. It is a subclass of ForkJoinTask, which is an abstract class representing a task that can be executed on a separate core in a multicore system. The RecursiveAction class is extended to create a task that has a void return type. The code that represents the compu
3 min read
Java.util.concurrent.Phaser class in Java with Examples
Phaser's primary purpose is to enable synchronization of threads that represent one or more phases of activity. It lets us define a synchronization object that waits until a specific phase has been completed. It then advances to the next phase until that phase concludes. It can also be used to synchronize a single phase, and in that regard, it acts
7 min read
Java.util.concurrent.RecursiveTask class in Java with Examples
RecursiveTask is an abstract class encapsulates a task that returns a result. It is a subclass of ForkJoinTask. The RecursiveTask class is extended to create a task that has a particular return type. The code that represents the computational portion of the task is kept within the compute() method of RecursiveTask. RecursiveTask class is mostly use
2 min read
Java.awt.image.RescaleOp Class in Java with Examples
RescaleOp is a class in java.awt.image package which implements BufferedImageOp and RasterOp interface. This class performs a pixel-by-pixel rescaling of the data in the source image by multiplying the sample values for each pixel by a scale factor and then adding an offset. The scaled sample values are clipped to the minimum/maximum representation
5 min read
Java class dependency analyzer in Java 8 with Examples
Java class dependency analyzer: jdeps is a new command-line tool introduced in JDK 8 to understand the static dependencies and libraries of application i.e. jdeps command shows the package-level or class-level dependencies of Java class files. The input for jdeps can be a .class file pathname, a JAR file or it can be a fully qualified class name to
2 min read
Java.util.BitSet class in Java with Examples | Set 1
BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values. Constructors: BitSet class Constructors / \ BitSet() BitSet(int no_Of_Bits)BitSet() : A no-argument constructor to create an empty BitSet object. BitSet(int no_Of_Bits): A one-constructor with an integer argument to create an instance of t
2 min read
Java.util.BitSet class methods in Java with Examples | Set 2
Methods discussed in this post: BitSet class methods. / / | | \ \ set() xor() clone() clear() length() cardinality() We strongly recommend to refer below set 1 as a prerequisite of this. BitSet class in Java | Set 1 set() : java.util.BitSet.set() method is a sets the bit at the specified index to the specified value. Syntax:public void set(int bitp
4 min read
Java.lang.Class class in Java | Set 1
Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It has no public constructor. Class objects are cons
15+ min read
Java.lang.Class class in Java | Set 2
Java.lang.Class class in Java | Set 1 More methods: 1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface. These modifiers are already decoded in Mo
15+ min read
Class getModule() method in Java with Examples
The getModule() method of java.lang.Class class is used to get the module of this entity. This entity can be a class, an array, an interface, etc. The method returns the module of the entity.Syntax: public Module getModule() Parameter: This method does not accept any parameter.Return Value: This method returns the module of the entity.Below program
2 min read
Class getPackage() method in Java with Examples
The getPackage() method of java.lang.Class class is used to get the package of this entity. This entity can be a class, an array, an interface, etc. The method returns the package of this entity.Syntax: public Package getPackage() Parameter: This method does not accept any parameter.Return Value: This method returns the package of this entity.Below
1 min read
Class getPackageName() method in Java with Examples
The getPackageName() method of java.lang.Class class is used to get the package name of this entity. This entity can be a class, an array, an interface, etc. The method returns the package name of this entity as a String.Syntax: public String getPackageName() Parameter: This method does not accept any parameter.Return Value: This method returns the
2 min read
Class getTypeParameters() method in Java with Examples
The getTypeParameters() method of java.lang.Class class is used to get the type parameters of this entity. This entity can be a class, an array, an interface, etc. The method returns an array of TypeVariable objects representing the type variables.Syntax: public TypeVariable&lt;Class&lt;T&gt;&gt; getTypeParameters() Parameter: This method does not
2 min read
Java Guava | compare() method of Short Class with Examples
Shorts.compare() method of Guava's Shorts Class is used to compare the two specified short values. These values are passed as the parameter and the result of comparison is found as the difference of 1st value and the 2nd value. Hence it can be positive, zero or negative. Syntax: public static int compare(short x, short y) Parameters: This method ac
2 min read
Class getDeclaredClasses() method in Java with Examples
The getDeclaredClasses() method of java.lang.Class class is used to get the classes of this class, which are the classes that are private, public, protected or default and its members, but not the inherited ones. The method returns the classes of this class in the form of array of Class objects. Syntax: public Class[] getDeclaredClasses() throws Se
2 min read
Class getDeclaredMethods() method in Java with Examples
The getDeclaredMethods() method of java.lang.Class class is used to get the methods of this class, which are the methods that are private, public, protected or default and its members or the members of its member classes and interfaces, but not the inherited methods. The method returns the methods of this class in the form of an array of Method obj
2 min read
Class getDeclaredFields() method in Java with Examples
The getDeclaredFields() method of java.lang.Class class is used to get the fields of this class, which are the fields that are private, public, protected or default and its members, but not the inherited ones. The method returns the fields of this class in the form of array of Field objects. Syntax: public Field[] getDeclaredFields() throws Securit
2 min read
Class getConstructors() method in Java with Examples
The getConstructors() method of java.lang.Class class is used to get the constructors of this class, which are the constructors that are public. The method returns the constructors of this class in the form of array of Constructor objects. Syntax: public Constructor[] getConstructors() Parameter: This method does not accept any parameter.Return Val
2 min read
Class getName() method in Java with Examples
The getName() method of java.lang.Class class is used to get the name of this entity. This entity can be a class, an array, an interface, etc. The method returns the name of the entity as a String.Syntax: public String getName() Parameter: This method does not accept any parameter.Return Value: This method returns the name of the entity as a String
1 min read
Class getGenericSuperclass() method in Java with Examples
The getGenericSuperclass() method of java.lang.Class class is used to get the type of the superclass of this entity. This entity can be a class, an array, an interface, etc. The method returns the type of the superclass of this entity.Syntax: public Type getGenericSuperclass() Parameter: This method does not accept any parameter.Return Value: This
2 min read
Class getClassLoader() method in Java with Examples
The getClassLoader() method of java.lang.Class class is used to get the classLoader of this entity. This entity can be a class, an array, an interface, etc. The method returns the classLoader of this entity.Syntax: public ClassLoader getClassLoader() Parameter: This method does not accept any parameter.Return Value: This method returns the ClassLoa
1 min read
Class getSuperclass() method in Java with Examples
The getSuperclass() method of java.lang.Class class is used to get the super class of this entity. This entity can be a class, an array, an interface, etc. The method returns the super class of this entity.Syntax: public Class&lt;T&gt; getSuperclass() Parameter: This method does not accept any parameter.Return Value: This method returns the super c
2 min read
Class isEnum() method in Java with Examples
The isEnum() method of java.lang.Class class is used to check if this Class is the Enum class. The method returns true if this Class is the Enum class. It returns false otherwise.Syntax: public boolean isEnum() Parameter: This method does not accept any parameter.Return Value: This method returns true if this Class is the Enum class. It returns fal
2 min read
Class isLocalClass() method in Java with Examples
The isLocalClass() method of java.lang.Class class is used to check if this Class is the Local class. The method returns true if this Class is the Local class. It returns false otherwise.Syntax: public boolean isLocalClass() Parameter: This method does not accept any parameter.Return Value: This method returns true if this Class is the Local class.
2 min read
Class isSynthetic() method in Java with Examples
The isSynthetic() method of java.lang.Class class is used to check if this Class is the Synthetic class. The method returns true if this Class is the Synthetic class. It returns false otherwise.Syntax: public boolean isSynthetic() Parameter: This method does not accept any parameter.Return Value: This method returns true if this Class is the Synthe
2 min read
Class isInterface() method in Java with Examples
The isInterface() method of java.lang.Class class is used to check if this Class is an interface. The method returns true if this Class is an interface. It returns false otherwise. Syntax: public boolean isInterface() Parameter: This method does not accept any parameter. Return Value: This method returns true if this Class is an interface. It retur
2 min read
Class isAnnotation() method in Java with Examples
The isAnnotation() method of java.lang.Class class is used to check if this Class is the Annotation type. The method returns true if this Class is the Annotation type. It returns false otherwise.Syntax: public boolean isAnnotation() Parameter: This method does not accept any parameter.Return Value: This method returns true if this Class is the Anno
2 min read
Class isMemberClass() method in Java with Examples
The isMemberClass() method of java.lang.Class class is used to check if this Class is the Member class. The method returns true if this Class is the Member class. It returns false otherwise.Syntax: public boolean isMemberClass() Parameter: This method does not accept any parameter.Return Value: This method returns true if this Class is the Member c
2 min read
Practice Tags :
three90RightbarBannerImg