Java Classes and Objects
Java classes and objects are the heart of object-oriented programming. A class is a blueprint that describes data (fields) and behaviour (methods); an object is a real instance built from that blueprint.
Creating a Class and Objects
Here a Dog class has two fields and one method. Each object created with new gets its own copy of the fields.
class Dog {
String name;
int age;
void bark() {
System.out.println(name + " says woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.name = "Rex";
d1.age = 3;
Dog d2 = new Dog();
d2.name = "Bolt";
d1.bark();
d2.bark();
System.out.println(d2.age);
}
}Rex says woof! Bolt says woof! 0
Fields get default values, so d2.age is 0 because we never set it. The dot operator accesses an object's fields and methods.
Constructors and this
A constructor is a special method with the class name and no return type. It runs when you call new and sets up the object. this refers to the current object and separates a field from a parameter with the same name.
class Book {
String title;
String author;
int pages;
Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
Book(String title) {
this(title, "Unknown", 0); // calls the other constructor
}
@Override
public String toString() {
return title + " by " + author + " (" + pages + " pages)";
}
}
public class Main {
public static void main(String[] args) {
Book a = new Book("Clean Code", "Robert Martin", 464);
Book b = new Book("Notes");
System.out.println(a);
System.out.println(b);
}
}Clean Code by Robert Martin (464 pages) Notes by Unknown (0 pages)
Overriding toString controls what println shows for your object. Without it you would see something like Book@5ca881b5.
Static vs Instance Members
Instance fields belong to each object. A static field belongs to the class itself and is shared by all objects. Static methods can be called without creating an object.
class Counter {
static int created = 0;
final int id;
Counter() {
created++;
id = created;
}
}
public class Main {
public static void main(String[] args) {
Counter a = new Counter();
Counter b = new Counter();
Counter c = new Counter();
System.out.println("Created: " + Counter.created);
System.out.println("c.id = " + c.id + ", a.id = " + a.id);
}
}Created: 3 c.id = 3, a.id = 1
Key Terms
- Class: the blueprint (Book).
- Object or instance: one thing made from the class (new Book(...)).
- Field: a variable that belongs to an object or class.
- Method: a function defined in a class.
- Constructor: code that initialises a new object.
Frequently asked questions
What is the difference between a class and an object in Java?
A class is a template that defines fields and methods. An object is a concrete instance of that class, created with new, holding its own field values.
What happens if I do not write a constructor?
Java provides a default no-argument constructor automatically. As soon as you write any constructor, that default is no longer generated.
Why can a static method not use this?
Static methods belong to the class, not to any object, so there is no current object for this to refer to.