The two most important words in Java are class and object. Get these clear and everything else follows (ICSE Class 9).
1Objects: state and behaviour
An object represents a real-world thing in a program. It bundles two things:
- State — the data it holds (its attributes), stored in member variables.
- Behaviour — what it can do, written as member methods.
For a Student object: state = name, marks; behaviour = display(), calculateGrade(). Bundling data with its methods is encapsulation.
- An object models a real thing and bundles state (data) + behaviour (actions).
- State lives in member variables; behaviour is written as member methods.
- Bundling data with its methods is encapsulation.
2Classes: blueprint, factory & message passing
A class is the design that describes what its objects have and can do. It is both a blueprint and an object factory:
class Student {
String name; // state
int marks;
void display() { // behaviour
System.out.println(name + " : " + marks);
}
}
Student a = new Student(); // one object (the factory makes it)
Student b = new Student(); // another objectMessage passing = objects communicate by calling each other's methods. Writing a.display() sends the message "display yourself" to object a.
- A class is a blueprint that describes its objects, and a factory that creates them with new.
- Each object has its own copy of the member variables.
- Objects communicate by message passing — calling each other's methods, e.g. a.display().
3A class is a user-defined data type
Java has built-in (primitive) types like int and char. When you write a class, you create a new data type of your own:
int x; // x is of the built-in type int Student s; // s is of the user-defined type Student
Because a class groups several values (and methods) together, it is called a composite (made of parts) data type — unlike a primitive, which holds a single simple value.
- Defining a class creates a new, user-defined data type (e.g. Student).
- It is a composite type — it groups several values and methods together.
- A primitive type holds one simple value; a class type holds an object.
★ Practical: build a class
In BlueJ, write and use a class:
- Write a class Book with member variables title (String) and price (double).
- Add a member method display() that prints both.
- Create two Book objects and call display() on each (message passing).
- State which variables are primitive types and which are user-defined.
Ready for the chapter test?
Answer 5 questions. Score 60% or more to mark this chapter complete.
Start the test →💡 Log in to save your progress and earn the certificate.