Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Class forName(String, boolean, ClassLoader) method in Java with Examples
Next article icon

Class forName(String, boolean, ClassLoader) method in Java with Examples

Last Updated : 01 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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<T>  forName(String className,           boolean initialize,           ClassLoader classLoader)   throws ClassNotFoundException


Parameter: This method accepts following parameters: 
 

  • className which is the Class for which its instance is required.
  • initialize which is the boolean to state if this class instance needs to be initialized or not.
  • classLoader which is the ClassLoader from which this Class must be loaded.


Return Value: This method returns the instance of this Class fetched using the specified parameters.
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
  • SecurityException: if a security manager is present, and the loader is null, and the caller's class loader is not null, and the caller does not have the RuntimePermission("getClassLoader")


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     {          // returns the Class object for this class         Class myClass = Class.forName("Test");          ClassLoader loader = myClass.getClassLoader();          // get the Class instance using forName method         Class c1             = Class.forName("java.lang.String",                             true,                             loader);          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     {          // returns the Class object for this class         Class myClass = Class.forName("Test");          ClassLoader loader = myClass.getClassLoader();          // get the Class instance using forName method         Class c1             = Class.forName("java.lang.Integer",                             false,                             loader);          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-boolean-java.lang.ClassLoader-
 


Next Article
Class forName(String, boolean, ClassLoader) method in Java with Examples

S

srinam
Improve
Article Tags :
  • Java
  • Java-lang package
  • Java-Functions
  • Java.lang.Class
Practice Tags :
  • Java

Similar Reads

    Class forName() method in Java with Examples
    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
    1 min read
    Class toString() method in Java with Examples
    The toString() method of java.lang.Class class is used to convert the instance of this Class to a string representation. This method returns the formed string representation. Syntax: public String toString() Parameter: This method does not accept any parameter. Return Value: This method returns the
    1 min read
    Boolean compare() method in Java with Examples
    The compare() method of Boolean class is a built in method in Java which is used to compare two boolean values. It is a static method, so it can be called without creating any object of the Boolean class i.e. directly using the class name. Syntax: Boolean.compare(boolean a, boolean b) Parameters: It
    2 min read
    Boolean equals() method in Java with examples
    The equals() method of Boolean class is a built in method of Java which is used check equality of two Boolean object. Syntax: BooleanObject.equals(Object ob) Parameter: It take a parameter ob of type Object as input which is the instance to be compared. Return Type: The return type is boolean. It re
    2 min read
    Boolean booleanValue() method in Java with examples
    The booleanValue() method of Boolean Class is a built in method in java which is used to return the primitive boolean value of instance which is used to call the method booleanValue(). Syntax BooleanObject.booleanValue() Return Value: It returns a primitive boolean value. Below are the examples to i
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences