Loading content...
Loading content...
You must understand these basic concepts:
Class
Object
Class Loading Concept
A class is a user-defined data type used to create objects.
It acts as a blueprint/template for creating objects.
java
class ClassName {
// members
}class → Java keyword
ClassName → Identifier
A class can contain:
Variables (data members)
Methods (functions)
Constructors
Blocks (static / instance)
Enums
Inner Classes
Interfaces
java
class Student {
int sid;
String name;
void display() {
System.out.println("Student Info");
}
}Here, Student defines a category/type.
An object is an instance of a class.
It represents a real-world entity and holds actual data.
Object is created using new keyword
Memory is allocated in Heap memory
Reference is stored in a reference variable
Created at runtime (dynamic memory allocation)
java
class Student {
int sid;
String sname;
String semail;
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.sid = 101;
s1.sname = "Anil";
s1.semail = "anil@gmail.com";
}
}Student → Class
new Student() → Object created in Heap
s1 → Reference variable (stores address of object)
A Class Loader loads classes into JVM memory when required.
It is part of JRE (Java Runtime Environment)
Not all classes are loaded at once
Classes are loaded dynamically when needed
java
Student s1 = new Student();JVM checks:
If class is not loaded → load first
Then create object
java
public static void main(String[] args)The class containing main() is loaded first
java
ClassName.staticMethod();If class is not loaded → JVM loads it first
Java allows loading classes at runtime using Class.forName()
java
Class.forName("ClassName");It loads class using String name
It is a static method
Belongs to java.lang.Class
java
ClassNotFoundExceptionOccurs when:
.class file is not found
java
class A {
static {
System.out.println("Static Block in class A");
}
private A() {}
}
public class Main {
public static void main(String[] args) throws Exception {
Class.forName("A");
}
}Class.forName("A") → Loads class A
Static block executes when class is loaded
Constructor is private → object cannot be created
Still, class loads successfully