In Java, a class is a blueprint that defines properties and behaviors, while an object is a real-world instance created from that class. Together, they form the foundation of Object-Oriented Programming (OOP).
A class is defined using the class keyword. Objects are created using the new keyword, which allocates memory at runtime.
// Defining a class in Java
class Student {
String name;
int age;
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
// Main class to create object
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Rahul";
s1.age = 20;
s1.display();
}
}
Name: Rahul
Age: 20
The Student object s1 accesses class variables and methods using the dot (.) operator.
Define the Class properties below, then click "new Car()" to create Objects.
(No objects created yet...)
Car with fields and a method