A class describes what its objects know (fields) and what they can do (methods). Objects are created from the class at run time.
Defining a Class
A class is declared with the keyword class, followed by its name and a body in braces. The body contains fields (instance variables), which hold the data of each object, and methods, which define its behaviour. For example, class Rectangle { int length, width; int area() { return length * width; } }. Each object has its own copy of the instance variables.
Definition: Constructor. A special method with the same name as its class and no return type, called automatically when an object is created to initialise it.
Methods
A method is declared as: type name(parameter-list) { body }. The type is the type of value it returns, or void if it returns nothing. Parameters receive the values passed in a call. A method can access the fields of its object directly. Java passes arguments by value: for primitives, a copy of the value is passed; for objects, a copy of the reference is passed, so the method can change the object but not make the caller's variable refer to a new object.
Creating Objects
An object is created with the new operator, which allocates memory and returns a reference: Rectangle r = new Rectangle();. The members are reached with the dot operator: r.length = 10; r.area();. Several references may point to the same object. An object that no reference points to is removed by the garbage collector.
Constructors
A constructor initialises an object when it is created. It has the same name as the class and no return type, not even void. If a class has no constructor, Java supplies a default constructor with no arguments. A parameterised constructor takes arguments to set the fields: Rectangle(int l, int w) { length = l; width = w; }. The keyword this refers to the current object, and is used when a parameter has the same name as a field: this.length = length;. One constructor can call another with this(...).
Access Control
The access modifiers control visibility: private members are visible only in the class, default (no modifier) in the package, protected in the package and subclasses, and public everywhere. Fields are usually private, with public getter and setter methods, which is encapsulation.
Table 3.1: Parts of a class
| Part | Purpose | Example |
|---|---|---|
| Field | Holds object data | int length; |
| Method | Defines behaviour | int area() |
| Constructor | Initialises the object | Rectangle(int l, int w) |
| this | Refers to the current object | this.length = length; |
Example 3.1: A class with constructors
Problem: Define a class Student with roll number, name and marks, a parameterised constructor and a display method. Create two students and display them.
Solution:
- Declare private fields roll, name and marks.
- Write Student(int roll, String name, double marks), using this to set the fields.
- Write display() to print the fields.
- In main, create two objects with new and call display().
Result: The program prints the details of both students.
public class StudentDemo {
public static void main(String[] args) {
Student s1 = new Student(101, "Asha", 88.5);
Student s2 = new Student(102, "Ravi", 74.0);
s1.display();
s2.display();
}
}
class Student {
private int roll;
private String name;
private double marks;
Student(int roll, String name, double marks) {
this.roll = roll;
this.name = name;
this.marks = marks;
}
void display() {
System.out.println(roll + " " + name + " " + marks);
}
}
Output
101 Asha 88.5
102 Ravi 74.0
Common mistakes
- Giving a constructor a return type, which makes it an ordinary method.
- Using an object reference before creating the object with new, which causes a NullPointerException.
- Forgetting this when a parameter hides a field of the same name.
Key takeaways
- A class has fields, methods and constructors.
- Objects are created with new and used with the dot operator.
- A constructor has the class name and no return type.
- this refers to the current object.
Practice questions
- What is a constructor?
- How is an object created?
- What does the keyword this refer to?
Answers to the practice questions
- A special method with the class name and no return type that initialises a new object.
- With the new operator, as in Student s = new Student();.
- The current object.
Sources
- Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit III
