Define your own types with fields, constructors, and methods, control access with modifiers, and understand static, final, and packages.
Why: a class describes a kind of thing — its data (fields) and what it can do (methods). An object is one actual thing made from that blueprint with the new keyword. Here every Dog has its own name.
public class Dog {
String name; // a field — data each Dog carries
void bark() { // a method — something a Dog can do
System.out.println(name + " says woof!");
}
}
// elsewhere:
Dog d = new Dog(); // make one Dog object
d.name = "Rex";
d.bark(); // "Rex says woof!"Why: a constructor runs when you write new, setting up the object so it starts in a valid state. It has the same name as the class and no return type. "this" refers to the object being built, separating the field from the parameter.
public class Dog {
String name;
int age;
Dog(String name, int age) { // constructor
this.name = name; // this.name is the field; name is the parameter
this.age = age;
}
}
Dog d = new Dog("Rex", 3); // name and age set in one stepWhy: modifiers control who can touch a field or method. private means only this class; public means anyone. The usual pattern (encapsulation) is to keep fields private and expose public methods, so the object controls how its data changes.
public class Account {
private double balance = 0; // hidden from outside
public void deposit(double amount) { // the controlled way in
if (amount > 0) balance += amount;
}
public double getBalance() { // read-only access
return balance;
}
}Why: a normal (instance) field belongs to each object — every Dog has its own name. A static field belongs to the class itself and is shared by all objects — useful for counters or constants. Call static methods on the class, not an object.
public class Dog {
static int totalDogs = 0; // shared across all Dogs
String name; // unique to each Dog
Dog(String name) {
this.name = name;
totalDogs++; // bump the shared counter
}
}
new Dog("Rex");
new Dog("Fido");
System.out.println(Dog.totalDogs); // 2 — read it on the classWhy: final means "cannot be reassigned." A final variable is a constant; a final field must be set once (usually in the constructor) and never changes. It makes intent clear and prevents accidental edits.
public class Circle {
static final double PI = 3.14159; // a shared constant
final double radius; // set once, then fixed
Circle(double radius) {
this.radius = radius;
}
double area() {
return PI * radius * radius;
}
}Why: a package is a folder-like namespace that groups related classes and keeps names from clashing. The package line goes at the top of a file and must match the folder path. To use a class from another package, you import it.
// File: src/com/example/model/Dog.java
package com.example.model;
public class Dog {
public String name;
}
// File: src/com/example/Main.java
package com.example;
import com.example.model.Dog; // pull in the class from the other package
import java.util.ArrayList; // built-in classes are imported the same way