Classes and Objects in Java
Classes and Objects in Java
Classes and objects are the two pillars everything else in this tutorial rests on. Get comfortable with the difference between them now, since every later chapter- constructors, inheritance, polymorphism- simply adds more capability on top of this same foundation.
What is a Class?
A class is a blueprint that defines what data and behaviour its objects will have. It does not represent any single, specific thing; it describes a category of things. A Car class describes what every car has (fields like colour and speed) and what every car can do (methods like accelerate() and brake()), without referring to one particular car.
public class Car {
// Fields (attributes)
String colour;
int speed;
// Method (behaviour)
void accelerate() {
speed = speed + 10;
}
}
By convention, class names in Java start with an uppercase letter and use PascalCase, such as Car, BankAccount, or StudentRecord.
What is an Object?
An object is a concrete instance of a class, created in memory at runtime. If Car is the blueprint, then myCar and yourCar are objects, each with their own actual colour and speed values, independent of each other. Changing myCar's speed does not affect whatsoever on yourCar.
Every object created from a class has its own copy of the class's instance fields, but the methods themselves are shared and simply operate on whichever object called them.
How to Create a Class and Object in Java
Creating an object from a class uses the new keyword, which allocates memory for the object and returns a reference to it. That reference is then stored in a variable so you can use it later.
public class Car {
String colour;
int speed;
void accelerate() {
speed = speed + 10;
System.out.println(colour + " car is now at " + speed + " km/h");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // create an object
myCar.colour = "Red";
myCar.speed = 0;
myCar.accelerate(); // Red car is now at 10 km/h
myCar.accelerate(); // Red car is now at 20 km/h
Car yourCar = new Car(); // a second, independent object
yourCar.colour = "Blue";
yourCar.accelerate(); // Blue car is now at 10 km/h
}
}
Notice how myCar and yourCar are completely independent. Each new Car() call creates a fresh object with its own fields, sitting at its own location in memory.
Fields, Methods, and Instances
Three terms get used constantly from this point forward, and it is worth pinning down exactly what each one means:
Term | Meaning |
Field | A variable declared inside a class that stores an object's data (also called an attribute or instance variable) |
Method | A block of code inside a class that defines behaviour; it can read and modify the object's fields |
Instance | Another word for a specific object created from a class; 'myCar is an instance of Car' |
A field declared without a value defaults to a type-appropriate value, 0 for numeric types, false for boolean, and null for object references, until you explicitly assign something else.
Quick Tip
Methods that only read or modify a single object's fields are called instance methods. Later you will meet static methods and fields, which belong to the class itself rather than to any one object, but for now, assume everything you write belongs to an instance unless stated otherwise.










