Skip to content
Computer ScienceISC Class 12 · Objects

Classes, Objects and Constructors in Java (ISC Class 12 Computer Science)

Updated 17 October 2026 · Draft, not yet fact-checked

An object is an instance of a class: it holds data (attributes, instance variables) and behaviour (methods). A constructor has the same name as the class, no return type, and runs automatically at new to initialise the object. Constructors can be overloaded, and this refers to the current object.

The class is only a blueprint; new runs the constructor to build an object, and the variable b holds a reference to it, not the object itself.

Illustration drawn for QuestionBuddy.

Understand Classes, Objects and Constructors in Java

Class and object. A class is a user-defined type that describes the attributes and methods common to a set of things. An object is one instance of it, created with new. Each object has its own copy of the instance variables. For example a Box class describes length and width and a method to find area; every Box object holds its own values.

Creating objects. Box b = new Box(4, 5); declares the reference b (of type Box) and creates the object. The variable holds a reference (the address) to the object, not the object itself.

Constructor. A special method with (i) the same name as the class, (ii) no return type, not even void, and (iii) it is called automatically by new. A default (no-argument) constructor is supplied by the compiler only when the class has no constructor of its own. A parameterised constructor sets the fields from its arguments. Constructors can be overloaded: several constructors with different parameter lists, chosen by the arguments in new.

**The this keyword.** this is the current object. It separates a field from a parameter of the same name (this.qty = qty;), and this(...) as the first statement of a constructor calls another constructor of the same class. Writing qty = qty; instead assigns the parameter to itself and leaves the field at its default value (0 for numbers).

Reference assignment. Box q = p; copies the reference, so p and q name the same object and a change through q is seen through p. new is needed to get a second object. The operator == compares references, so two different objects with equal contents give false.

Default values of fields: int 0, double 0.0, boolean false, char the null character, and object references null.

Scanner input and StringTokenizer are in the next topic; static members and arguments are in the Methods chapter.

Classes, Objects and Constructors in Java: key facts

Constructor rules
  • Same name as the class
  • No return type, not even void
  • Runs automatically at new
  • Can be overloaded
A class with no constructor gets a default one from the compiler.
Creating an object
  • ClassName ref = new ClassName(arguments);
  • Reference ref holds the address
  • Each object has its own instance variables
Declaring the reference alone creates no object.
this keyword
  • this.field = parameter;
  • this(args) calls another constructor
  • It must be the first statement in the constructor
this is the current object.
Reference versus object
  • q = p copies the reference
  • p == q is true for the same object
  • Two new objects with equal data give == false
Use a method such as equals to compare contents.

How to answer Classes, Objects and Constructors in Java questions

Use this routine for class design and output questions.

  1. 1List the attributes as private or default instance variables with proper types.
  2. 2Write the constructor(s): same name as the class, no return type, assign every field.
  3. 3Write the methods for the behaviour; use return for computed values.
  4. 4In main, create each object with new, call the methods and print the results.
  5. 5When tracing, track each reference separately: note which variables name the same object.
  6. 6Check the output formatting (spaces, println versus print).

Classes, Objects and Constructors in Java in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Library book object

Suppose a library program creates a Book object with title and number of copies, and a method issue() reduces the copies by one while any remain.

A book is created with 2 copies. How many issue() calls succeed before it returns false?

Try it first, then show the solutionSolution
  1. Each successful call lowers copies by one: 2 to 1, then 1 to 0.
  2. With 0 copies the condition copies > 0 is false.

Answer: Two calls succeed; the third returns false.

Memory hook: Each object keeps its own stock count.

Two names for one account

Suppose a banking program writes Account y = x; and then changes y's balance.

Does x's balance change?

Try it first, then show the solutionSolution
  1. y = x copies the reference, so there is only one object.
  2. A change through y is the same as a change through x.

Answer: Yes, x sees the new balance; both names refer to one object.

Memory hook: Copying a reference copies the address, not the house.

Tricks to remember Classes, Objects and Constructors in Java

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • Class = blueprint, object = house

    Many houses can be built from one blueprint, each with its own colour.

    Use it when: Class versus object.

  • Constructor has no type

    No return type, name equals class: it looks like a method with the type missing.

    Use it when: Recognising constructors.

  • this = me

    this.x is "my x"; plain x may be the parameter.

    Use it when: Name clash between field and parameter.

  • new makes, = shares

    new makes an object; assignment only makes another name for it.

    Use it when: Reference tracing questions.

Classes, Objects and Constructors in Java: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: Constructor property · 1 mark · MCQBoard level

Which statement about a constructor is correct?
(a) it must return a value (b) it has the same name as its class (c) it is called with the dot operator (d) it can be declared static

Try it first, then show the solutionSolution
  1. A constructor has no return type and is called by new.
  2. It takes the name of its class.

Answer: (b) it has the same name as its class

Example 2: Overloaded constructors · 3 marks · Short answerHarder board level

Write the output:

class Box {
    int l, w;
    Box() { l = 1; w = 1; }
    Box(int x) { l = x; w = x; }
    Box(int x, int y) { l = x; w = y; }
    int area() { return l * w; }
}
public class Test {
    public static void main(String[] args) {
        Box a = new Box();
        Box b = new Box(4);
        Box c = new Box(3, 5);
        System.out.println(a.area() + " " + b.area() + " " + c.area());
    }
}
Try it first, then show the solutionSolution
  1. new Box() calls the no-argument constructor: l = 1, w = 1, area 1. (1 mark)
  2. new Box(4) sets l = w = 4, area 16; new Box(3, 5) sets l = 3, w = 5, area 15. (1 mark)
  3. The expression joins int values with spaces: 1 then a space then 16 then a space then 15. (1 mark)

Answer: 1 16 15

Example 3: Reference assignment · 3 marks · Short answerHarder board level

Using the Box class above, write the output:

Box p = new Box(2, 3);
Box q = p;
q.l = 10;
System.out.println(p.l + " " + p.area());
q = new Box(1, 1);
q.l = 7;
System.out.println(p.l + " " + q.l);
Try it first, then show the solutionSolution
  1. q = p makes q name the same object, so q.l = 10 sets p.l to 10; area = 10 x 3 = 30, printed as "10 30". (1.5 marks)
  2. q = new Box(1, 1) points q to a new object; q.l = 7 changes only that object. (1 mark)
  3. p.l is still 10 and q.l is 7: "10 7". (0.5 mark)

Answer: 10 30
10 7

Example 4: The this keyword · 3 marks · Short answerHarder board level

The class Item has a constructor Item(int qty, double price) { qty = qty; price = price; } and a method double total() { return qty * price; }, where qty and price are fields. Find total() for new Item(3, 2.5), explain the fault, and give the corrected constructor body.

Try it first, then show the solutionSolution
  1. In the constructor qty and price mean the parameters, so each statement assigns a parameter to itself. (1 mark)
  2. The fields stay at their default values 0 and 0.0, so total() returns 0 x 0.0 = 0.0. (1 mark)
  3. Corrected body: this.qty = qty; this.price = price; then total() = 3 x 2.5 = 7.5. (1 mark)

Answer: total() returns 0.0 with the fault; with this.qty = qty; this.price = price; it returns 7.5.

Example 5: Case: library issue counter · 4 marks · Case-basedHOTS / competency

Suppose a library uses this class:

class Book {
    String title; int copies;
    Book(String t, int c) { title = t; copies = c; }
    boolean issue() {
        if (copies > 0) { copies--; return true; }
        return false;
    }
}

main contains:

Book b = new Book("Maths", 2);
System.out.println(b.issue());
b.issue();
System.out.println(b.issue() + " " + b.copies);

(i) Write the output. (2 marks)
(ii) Name the attributes and the behaviour of the object. (1 mark)
(iii) Is Book b = null; b.issue(); valid at run time? (1 mark)

Try it first, then show the solutionSolution
  1. (i) First call: copies 2 to 1, prints true. Second call: copies 1 to 0. Third call: copies is 0, returns false, so the second print line is "false 0". Output: true, then false 0.
  2. (ii) Attributes: title and copies. Behaviour: the method issue().
  3. (iii) It compiles, but b holds no object, so the call throws a NullPointerException.

Answer: (i) true
false 0 (ii) title and copies; issue() (iii) compiles but throws NullPointerException

Where students lose marks in Classes, Objects and Constructors in Java

The slips examiners see most often, and what to write instead.

  • Mistake: Writing a return type on the constructor

    A method with a return type, even void, is an ordinary method, not a constructor.

    Wrong working

    void Box(int x) { l = x; }

    Right working

    Box(int x) { l = x; }

    Do this: No return type for constructors.

    Usually costs: 1 mark

  • Mistake: Shadowing the field by the parameter

    qty = qty assigns the parameter to itself and the field remains 0.

    Wrong working

    Item(int qty) { qty = qty; }

    Right working

    Item(int qty) { this.qty = qty; }

    Do this: Use this.field or different parameter names.

    Usually costs: 1 mark

  • Mistake: Thinking q = p makes a copy

    Only the reference is copied, so both names change the same object.

    Wrong working

    After q = p; q.l = 10; p.l is still 2

    Right working

    After q = p; q.l = 10; p.l is 10

    Do this: A second object needs new.

    Usually costs: 1 mark

Exam tips

  • Name the constructor exactly as the class, with matching capitals.
  • In an output question draw boxes for objects and arrows for references.
  • Initialise every attribute in the constructor when the question asks for a complete class.
  • Write return types and parameter types in every method header.
  • If you use this(...) in a constructor, make it the first statement.

Practice questions on Classes, Objects and Constructors in Java

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    Which statement creates an object of class Box?
    (a) Box b; (b) Box b = Box(); (c) Box b = new Box(); (d) new Box b;

    Hint

    Look for the new keyword and the constructor call.

    Answer

    (c) Box b = new Box();

  2. Q2 · 2 marks · Short answerBoard level

    Distinguish between a class and an object, and between an instance variable and a local variable.

    Hint

    Think blueprint, instance, and where the variable lives.

    Answer

    A class is the template that defines attributes and methods; an object is one instance created from it with new. An instance variable is declared in the class outside any method and each object has its own copy; a local variable is declared inside a method or block and exists only while that code runs.

  3. Q3 · 4 marks · Short answerHarder board level

    Write a class Rectangle with a constructor taking length and breadth, and methods area() and perimeter(). Show the output for new Rectangle(6, 4).

    Hint

    Use this.field if the names clash.

    Answer
    class Rectangle {
        int length, breadth;
        Rectangle(int length, int breadth) {
            this.length = length;
            this.breadth = breadth;
        }
        int area() { return length * breadth; }
        int perimeter() { return 2 * (length + breadth); }
    }

    With new Rectangle(6, 4): area() = 24 and perimeter() = 2 x (6 + 4) = 20.

  4. Q4 · 3 marks · Short answerHarder board level

    Write the output:

    class Counter {
        int c;
        Counter() { this(5); }
        Counter(int n) { c = n * 2; }
    }
    // in main
    Counter x = new Counter();
    Counter y = new Counter(3);
    System.out.println(x.c + " " + y.c);
    Hint

    this(5) calls the other constructor.

    Answer

    new Counter() runs this(5), so c = 5 x 2 = 10. new Counter(3) gives c = 6. Output: 10 6

  5. Q5 · 3 marks · Case-basedHOTS / competency

    Using the Box class from the worked examples, predict the output and justify each value:

    Box a = new Box(2, 2);
    Box b = new Box(2, 2);
    System.out.println((a == b) + " " + (a.area() == b.area()));
    Hint

    What does == compare for objects?

    Answer

    a == b is false because it compares the references and a and b are two different objects. a.area() == b.area() compares the int values 4 and 4, which is true. Output: false true

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.What is the return type of a constructor?Show answer

    None, not even void.

  2. 2.What does new do?Show answer

    Creates an object, allocates memory and calls the constructor.

  3. 3.What is constructor overloading?Show answer

    Having several constructors in one class with different parameter lists.

  4. 4.What does this refer to?Show answer

    The current object.

  5. 5.What is the default value of an int field?Show answer

    0.

  6. 6.What does == compare for two object references?Show answer

    Whether they refer to the same object.

Classes, Objects and Constructors in Java: frequently asked questions

What is a constructor in Java?

A special member method with the class name and no return type that initialises the object and is called automatically when the object is created with new.

What is the difference between a class and an object?

A class is the blueprint describing attributes and behaviour; an object is a particular instance of the class with its own attribute values.

What happens if a class has no constructor?

The compiler supplies a default no-argument constructor that gives the fields their default values. Once you write any constructor, that default is not supplied.

Can a constructor be called like a normal method?

No. It runs only through new, or through this(...) from another constructor in the same class.