Java Programming · Lesson 7 of 15 · Updated ·
Classes and Objects in Java — Constructors, this, static and Encapsulation
A class is a blueprint; an object is a real thing built from it. Java programs are made of classes that work together.
1. A class with fields, a constructor and methods
class Student {
String name; // fields (instance variables)
int marks;
Student(String name, int marks) { // constructor
this.name = name; // this.name = the field
this.marks = marks;
}
String grade() {
return marks >= 85 ? "A" : "B";
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student("Asha", 92);
Student t = new Student("Ravi", 74);
System.out.println(s.name + " " + s.grade() + ", " + t.name + " " + t.grade());
}
}
Asha A, Ravi B
new Student(...)creates an object on the heap and runs the constructor.- A constructor has the same name as the class and no return type.
thismeans "this object" — it separates the fieldthis.namefrom the parametername.
2. Constructors: default, overloaded, chained
If you write no constructor, Java gives a default no-argument one. Once you write any constructor, that default disappears.
class Product {
String name;
double price;
Product() {
this("Unknown", 0); // calls the other constructor
}
Product(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return name + " ₹" + price;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(new Product());
System.out.println(new Product("Pen", 10));
}
}
Unknown ₹0.0
Pen ₹10.0
toString() decides what println(object) shows. Without it you get something like Product@1b6d3586.
3. static: belongs to the class, not to objects
class Counter {
static int created = 0; // one copy shared by all objects
int id; // each object has its own
Counter() {
created++;
id = created;
}
static void report() { // static method: no "this", can't use id
System.out.println("objects created: " + created);
}
}
public class Main {
public static void main(String[] args) {
Counter a = new Counter(), b = new Counter(), c = new Counter();
System.out.println(a.id + " " + c.id);
Counter.report();
}
}
1 3
objects created: 3
main is static because the JVM calls it before any object exists.
4. Encapsulation: private fields + getters/setters
Hide the data (private) and allow access only through methods that can check the values.
class BankAccount {
private double balance;
public double getBalance() { return balance; }
public void deposit(double amount) {
if (amount <= 0) throw new IllegalArgumentException("amount must be positive");
balance += amount;
}
public boolean withdraw(double amount) {
if (amount > balance) return false;
balance -= amount;
return true;
}
}
public class Main {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.deposit(1000);
System.out.println(acc.withdraw(300) + " " + acc.withdraw(5000) + " " + acc.getBalance());
}
}
true false 700.0
Nobody can write acc.balance = -500 from outside — the class protects its own rules.
Access modifiers
| Modifier | Visible in |
|---|---|
private | the same class only |
| (none) "default" | the same package |
protected | same package + subclasses |
public | everywhere |
Common mistakes
- Writing
name = name;in a constructor (assigns the parameter to itself). Usethis.name = name;. - Adding a return type to a constructor (it becomes a normal method).
- Using instance fields inside a static method.
Quick recap
- Class = fields + constructors + methods;
newcreates objects. thisrefers to the current object;this(...)chains constructors.staticmembers belong to the class and are shared.- Encapsulation: private fields + public methods that check rules.
- Override
toString()for readable output.