Updates
  • Starting New Weekday Batch for Full Stack Java Development on 15 September 2025 @ 02:00 PM to 04:00 PM
  • Starting New Weekday Batch for MERN Stack Development on 29 September 2025 @ 04:00 PM to 06:00 PM
Join Course

Introduction

Object Oriented Programming defines how concepts relate to real world. Prior to starting with OOPs, we need to familiarize ourselves with few related concepts:

• Class
• Object
• Class Loading Concept

Class: -

A class is a user-defined data type.

Syntax: -

class <class_name>{ }

Here, class is a Java keyword and class name is an Identifier.

Here we have a list of members which we can declare into a class.

a. Variable
b. Block
c. Method
d. Constructor
e. Enum
f. Inner Class
g. Interface

In Java a class is basically used to define a category.

Example:- Let’s assume we are developing a module which stores three types of data of students and these are: Student Id, Student Name and Student Email.

Student Form: -

In a student form we have to define the category of the student, as per the given criteria all users who fill this form along with their data will be considered a student. To define the student category we need to create a class which is student and inside the class we define the relevant fields.

Class Student{
int sid;
String sname;
String semail;
}

In the above code we can see we have a student class having three fields sid, sname and scity which we will store in Student Form data.

Object: -

• An object is a blue-print of a class or we can say an object is a memory allocation which stores corresponding class information.
• An object gets its memory inside heap and its references are stored inside the reference variable.
• Object works on dynamic memory allocation or runtime memory allocation concepts; this implies that object size is not definite.

Example: -

class Student{
int sid;
String sname;
String semail;
}

Student s1 = new Student ();

In the above example we have a class which is student and then we are creating an object of student class “Student s1 = new Student ()”, here s1 is a reference variable and new Student () is an object of Student class and their reference is stored inside s1.

Class Loading Concept: -

Class Loaders are responsible for loading a class dynamically into the JVM Memory and Class Loader is a part of JRE (Java Runtime Environment). In Java all the classes are not loaded into the JVM memory at a time, a class loads when an application is required.

There are certain situations when a class loads into the JVM memory.

1. When we create an object of a class then JVM checks that whether the class is loaded into the JVM memory or not, if it is not loaded then the loader loads that class first then the object is created, if that class is already loaded then the loader dose not load the same class again into the JVM memory.
2. When we run a .class file which has the main () then loader loads that class first and thereafter the main method gets executed.
3. When we access a static member using class name or class reference variable that has null value then loader loads that class first if it is not already loaded.

Dynamic Class Loading: -

In Java, we can dynamically load a class according to the business logic. There is a class called java.lang.Class, and within this class, we have a method called forName():

public static java.lang.Class forName(java.lang.String) throws java.lang.ClassNotFoundException;

As we can see, the forName() method is a static method that takes a String parameter. We can invoke this method by providing the class name as a String argument. When invoking this method, it is necessary to pass a String argument representing the class name we intend to load.

The ClassNotFoundException occurs when an attempt is made to load a class, but the corresponding .class file is not found at the specified location. In such cases, a ClassNotFoundException is thrown during the execution of the program.

            
class A{
	static{
		System.out.println("SB in class A");
	}

	private A(){}
}
class JTC{
	public static void main(String arg[]) throws Exception{
		Class.forName("A");
	}
}
    SB in class A
            
            
class A{
	static{
		System.out.println("SB in class A");
	}

	private A(){}
}
class JTC{
	public static void main(String arg[]) throws Exception{
		Class.forName("ABC");
	}
}
    Exception in thread "main" java.lang.ClassNotFoundException: ABC
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at JTC.main(Test.java:10)