- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Load class with forName() method in Java
The class object associated with the class with the given string name can be returned with the method java.lang.Class.forName(String name, boolean initialize, ClassLoader loader), using the class loader that is used to load the class.
The parameters in the forName() method are name, initialize and loader. If the value of the parameter loader is null, then the class is loaded using the bootstrap class loader. Also, if the initialize parameter is true, then only the class is initialized if it has not been initialized earlier.
A program that loads the class using the forName() method is given as follows −
Example
import java.lang.*; public class Demo { public static void main(String[] args) { try { Class c1 = Class.forName("Demo"); ClassLoader classLoader = c1.getClassLoader(); Class c2 = Class.forName("java.lang.String", true, classLoader); System.out.println("Class = " + c1.getName()); System.out.println("Class = " + c2.getName()); } catch(ClassNotFoundException e) { System.out.println("Excepton: " + e.toString()); } } }
Output
Class = Demo Class = java.lang.String
- Related Articles
- Class declaration with one method in Java
- Class declaration with a method that has a parameter in Java
- NavigableSet Class floor() method in Java
- NavigableSet Class higher() method in Java
- NavigableSet Class lower() method in Java
- Method of Object class in Java
- NavigableSet Class ceiling() method in Java
- How to resolve "Could not find or load main class package" in Java?
- HTML DOM Video load( ) Method
- The indexOf() method of CopyOnWriteArrayList class in Java
- The addAll() method of AbstractList class in Java
- The lastIndexOf() method of AbstractList class in Java
- The subList() method of AbstractList class in Java
- The indexOf() method of AbstractList class in Java
- The equals() method of AbstractList class in Java

Advertisements