What are Classes and Objects?
A class is a blueprint or template that defines the properties (fields) and behaviors (methods) of objects. An object is a specific instance of a class.
java
// Class — the blueprint
public class Car {
String brand;
String color;
int speed;
void accelerate() {
speed += 10;
System.out.println(brand + " speed: " + speed + " km/h");
}
}
// Object — an instance
Car myCar = new Car();
myCar.brand = "Toyota";
myCar.color = "Red";
myCar.accelerate(); // Toyota speed: 10 km/hConstructors
A constructor is a special method that initializes an object when it's created:
java
public class Student {
String name;
int age;
// Constructor
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// Default constructor
public Student() {
this.name = "Unknown";
this.age = 0;
}
void display() {
System.out.println(name + ", Age: " + age);
}
}
// Usage
Student s1 = new Student("Arjun", 22);
Student s2 = new Student(); // Uses default constructor
s1.display(); // Arjun, Age: 22
s2.display(); // Unknown, Age: 0The this Keyword
this refers to the current object:
java
public class Employee {
String name;
double salary;
public Employee(String name, double salary) {
this.name = name; // this.name = instance variable
this.salary = salary; // name = parameter
}
}Access Modifiers
| Modifier | Class | Package | Subclass | World |
|---|---|---|---|---|
public | ✓ | ✓ | ✓ | ✓ |
protected | ✓ | ✓ | ✓ | ✗ |
| (default) | ✓ | ✓ | ✗ | ✗ |
private | ✓ | ✗ | ✗ | ✗ |
Getters and Setters (Encapsulation)
java
public class BankAccount {
private double balance;
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}Static Members
static members belong to the class, not individual objects:
java
public class Counter {
static int count = 0;
public Counter() {
count++;
}
public static int getCount() {
return count;
}
}
new Counter();
new Counter();
new Counter();
System.out.println(Counter.getCount()); // 3Practical Example
java
public class Product {
private String name;
private double price;
private int quantity;
private static int totalProducts = 0;
public Product(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
totalProducts++;
}
public double getTotalValue() {
return price * quantity;
}
public void display() {
System.out.printf("%-15s ₹%8.2f x %d = ₹%.2f%n",
name, price, quantity, getTotalValue());
}
public static void main(String[] args) {
Product[] products = {
new Product("Laptop", 65000, 2),
new Product("Mouse", 500, 10),
new Product("Keyboard", 1500, 5),
};
System.out.println("=== Inventory ===");
double grandTotal = 0;
for (Product p : products) {
p.display();
grandTotal += p.getTotalValue();
}
System.out.printf("%nTotal Products: %d%n", totalProducts);
System.out.printf("Grand Total: ₹%.2f%n", grandTotal);
}
}Summary
- A class is a blueprint; an object is an instance of a class
- Constructors initialize objects — same name as the class, no return type
- Use
thisto refer to the current object's fields - Encapsulation: use
privatefields withpublicgetters/setters - Static members are shared across all objects of a class
- Java supports constructor overloading (multiple constructors with different parameters)