Constructors | ICSE Class 10 Computer Applications Notes
On this page
This note covers constructors in Java, object initialisation, constructor characteristics and syntax, default and parameterised constructors, constructor overloading, differences between constructors and methods, and the steps used to trace object creation.
What is a constructor and why does an object need initialisation?
A class describes a kind of object through its data and operations. An object is an instance of a class, meaning a particular object created from that class definition. Java is the programming language used here.
A variable is a named storage location for a value. A data member, also called a field, is a variable declared as part of a class. An instance variable is a field for which each object has its own storage.
The values held in an object's instance variables form its state. A method is a named routine that performs an operation. Methods describe behaviour, while instance variables record the data on which that behaviour can operate.
Definition: A constructor is a special routine declared in a class and used during object creation to initialise the new object. Initialisation means establishing starting values or a starting condition.
How does construction connect data and behaviour?
A constructor places the initialisation instructions alongside the class definition. This connects the way an object begins with the data that the class declares. Later method calls can work with the state established during construction.
Invocation means causing a routine to execute. When a suitable constructor is invoked during object creation, its instructions run for that new object. Declaring a constructor describes those instructions; declaration alone does not execute them.
The distinction between a class and an object matters here. A class definition supplies the common structure. Each newly created object has its own instance variables, so the same constructor declaration can initialise separate objects when they are created.
What does a constructor contribute?
A constructor can make the intended starting state explicit. It can accept information supplied when the object is created or establish a starting state without such information. A parameterised constructor receives supplied values through parameters; a no-argument constructor receives none. A parameter is a receiving variable declared by the constructor.
Keep the constructor's role separate from later operations. Construction establishes an object's starting condition. Methods can subsequently inspect or change its state. A constructor does not have to display anything for initialisation to have taken place successfully.
What characteristics and syntax identify a Java constructor?
Syntax means the rules for arranging the parts of a program. A constructor declaration has the same name as its class, a parameter list enclosed in parentheses, and a body enclosed in braces. Its name must match the class name exactly.
A type specifies the kind of value a variable can hold. A parameter is a variable declared to receive a value supplied during invocation. A parameter list gives the declared types and names of these receiving variables. The list may be empty when the constructor needs no supplied arguments.
A return type states what kind of value a method sends back. The Java word void indicates that a method does not return a value. A constructor has no return type in its declaration, including no void.
Rule: Match the constructor name to the class name and omit a return type. A declaration with a return type is not a constructor merely because its name matches the class.
How should the general syntax be read?
An access modifier controls where a declaration can be used. The keyword public provides public access. A keyword is a word reserved by Java for a particular purpose. A constructor may be declared public, but public is not what identifies it as a constructor.
The symbols ( ) enclose the parameter list, and { } enclose the body. A statement is an instruction in the program. A semicolon, ;, ends an ordinary assignment statement. Italic phrases in the following pattern are placeholders, not Java identifiers.
Syntax: public class name(parameter declarations) { initialisation statements }
An identifier is a name used for a program element. Replace the class-name placeholder with the actual class identifier. Replace the parameter declarations with the required types and identifiers, or leave the parentheses empty for a no-argument constructor.
The constructor body contains the required initialisation instructions. Its braces are needed even when the body contains no statements. The pattern describes the parts of a declaration; the italic explanatory phrases themselves must not be typed as program instructions.
| Part to inspect | Constructor characteristic |
|---|---|
| Name | Matches the enclosing class name exactly |
| Return type | Absent, including no void |
| Parameter list | May be empty or contain parameter declarations |
| Body | Contains instructions used during construction |
How is a constructor invoked when an object is created?
The Java operator new is used in a class-instance creation expression. An expression is a combination of program elements evaluated to produce a value. For an ordinary class, new is followed by the class name and an argument list.
An argument, also called an actual parameter, is a value supplied in an invocation. A formal parameter is the receiving variable in the declaration. The argument list belongs to the creation expression; the formal parameter list belongs to the constructor declaration.
Syntax: new class name(arguments)
The italic phrases again denote placeholders. The parentheses contain the supplied arguments, or remain empty when no arguments are supplied. The form must correspond to an accessible constructor that can accept those arguments.
What is the difference between an object and a reference?
A reference is a value that refers to an object. A reference variable can hold such a value. The result of a successful new expression is a reference to the newly created object, which can be stored in a suitable variable.
The symbol = is the assignment operator: it stores the value on its right in the variable on its left. It is not an equality test. A declaration introduces a variable and its type; the declared type specifies the kind of value it can hold.
Syntax: class name reference variable = new class name(arguments);
In this pattern, the left-hand class name gives the reference variable's type. The new expression creates and initialises the object. Assignment stores the resulting reference. Merely declaring a reference variable, without an object-creation expression, does not itself create an object.
How should the construction step be followed?
- Identify the class named after new and locate its constructor declarations.
- Read the supplied arguments, keeping their order and types clear.
- Find the applicable accessible constructor and associate each argument with its formal parameter.
- Follow the selected constructor's instructions for the new object before continuing with subsequent statements.
Output is information displayed by the program. A constructor may initialise fields without producing output. To predict displayed information, follow the program's output instructions rather than assuming that object creation prints the object's values.
What is a default constructor and when is it supplied?
A compiler translates source code and checks language rules. Source code is the program text written by the programmer. In Java, the compiler supplies a default constructor when a class declares no constructor of its own.
This automatically supplied constructor takes no arguments. Its availability depends on the absence of constructor declarations, not on whether the programmer has written a method or declared fields. Methods and fields do not count as constructor declarations.
Rule: Declaring any constructor prevents the compiler from supplying a default constructor. If a no-argument constructor is then required, it must be declared explicitly.
How is this different from default field initialisation?
Default field values are the values Java gives fields before explicit initialisation takes effect. This language rule is separate from the rule that supplies a default constructor. An explicitly declared constructor does not remove default field initialisation.
A primitive type directly represents a basic value. The primitive type int holds whole numbers, double holds floating-point numbers, boolean holds truth values, and char holds a single character value. Floating-point representation supports numbers with fractional parts.
| Instance-field type | Default value | Meaning |
|---|---|---|
| int | 0 | Integer zero |
| double | 0.0 | Floating-point zero |
| boolean | false | The false truth value |
| char | '\u0000' | The zero-valued character |
| Reference type | null | No object reference |
The notation '\u0000' is Java character-literal notation for the character with value zero; it is not the digit character. A literal writes a value directly in source code. null denotes the absence of an object reference.
An explicit initialiser supplies a value in a field declaration. That initialiser, or an assignment performed during construction, can replace a field's default value. Therefore, the table alone cannot determine the final state when further initialisation instructions are present.
Which variables does this rule concern?
A local variable is declared within a routine or block. The automatic default-value rule for fields must not be applied to local variables. A local variable needs to have been assigned a value before its value is read.
Do not explain default values by saying that the default constructor writes an assignment for every field. Field defaults and the provision of a constructor are distinct rules. Keeping them separate also explains why untouched fields can retain defaults in a programmer-written constructor.
How do no-argument and parameterised constructors differ?
A no-argument constructor has an empty parameter list. A programmer can explicitly declare one and place initialisation instructions in its body. Its lack of parameters does not mean its body must be empty or that it cannot assign field values.
A parameterised constructor declares parameters that receive arguments when it is invoked. These values can be used to initialise the new object's fields. Its declaration states the required parameter types and their order.
Note: No-argument describes a constructor's parameter list. Default describes the constructor supplied automatically when the class declares none. An explicitly written no-argument constructor is not compiler-supplied.
What choice does each form provide?
An explicit no-argument constructor can establish a starting state through instructions already written in its body. A parameterised constructor can establish a starting state using information supplied by the code that creates the object.
Both forms concern initialisation. Neither form needs to display output. A parameterised constructor is not a method for changing an existing object merely because it receives values; its role remains part of constructing the object.
| Feature | Explicit no-argument constructor | Parameterised constructor |
|---|---|---|
| Formal parameters | None | Declared receiving variables |
| Arguments at invocation | Empty argument list | Arguments compatible with the declaration |
| Initialisation instructions | May assign values without arguments | May use arguments to assign values |
| Who declares it? | The programmer | The programmer |
What happens when a class has only a parameterised constructor?
The compiler does not add a default constructor alongside it. An object-creation expression with no arguments cannot use that parameterised constructor when its required arguments are missing. Constructor availability must be checked against the declarations actually present.
When both forms are explicitly declared, the class supports both corresponding ways of construction, subject to access rules. A compatible argument is one whose type can be accepted by the corresponding parameter under Java's conversion rules.
Do not choose between constructors by the number of assignments in their bodies. Classification as no-argument or parameterised comes from the parameter list. The body determines what initialisation actually occurs after the relevant constructor has been selected.
How do parameters supply values to instance variables?
Constructor parameters and instance variables have different roles. A formal parameter receives an argument for a particular invocation. An instance variable belongs to the object and can preserve the assigned value after that invocation has finished.
Merely declaring a parameter does not automatically copy it into a field. The constructor body must perform the intended assignment or other initialisation operation. Trace what the statements do instead of treating similarly named variables as interchangeable.
How does an assignment transfer a supplied value?
For a primitive parameter, Java passes the argument's value into the parameter. This is pass by value: the parameter receives a copy of the supplied value. Assigning that parameter to an instance field stores the value in the object.
The general relationship is instance field = parameter;, with the words standing for identifiers in an actual program. The left side is the storage being changed. The right side supplies the value. Reversing the sides changes the meaning of the instruction.
A constructor parameter is not itself an instance field. The same constructor declaration can be invoked for different new objects, each with its own instance storage. Follow the receiving object as well as the supplied values when analysing construction.
What if a parameter and field share a name?
Scope is the region of a program where a name can be used for its declaration. If a parameter has the same name as an instance field, the parameter hides the field's simple name within its scope.
The keyword this refers to the current object. The dot symbol, ., selects a member. Writing this followed by a dot and a field name identifies that object's field, distinguishing it from a same-named parameter.
Syntax: this.field name = parameter name;
Here the italic phrases are placeholders for the required identifiers. When the field and parameter share an identifier, this distinguishes the destination field from the parameter supplying the value. It is not a different field created by the constructor.
Check assignment direction carefully. A statement that merely assigns a parameter to itself does not initialise the intended field. Naming and scope therefore matter even when the program contains an assignment sign and can be compiled.
What is constructor overloading and how is a constructor selected?
Constructor overloading means declaring constructors in the same class with different parameter lists. They share the class name but provide different ways to initialise its objects. Their bodies may contain different initialisation instructions.
For the declarations considered here, a constructor signature consists of its name and ordered parameter types. Ordered means that the positions of the types matter. Parameter identifiers do not distinguish otherwise identical signatures.
Rule: Overloaded constructors must differ in their parameter lists. Changing parameter names, access modifiers or body statements alone does not create a distinct constructor signature.
Which differences make valid overloads?
| Difference between declarations | Can it distinguish overloads? | Reason |
|---|---|---|
| Number of parameters | Yes | The parameter lists have different lengths |
| Parameter types | Yes | The ordered type lists differ |
| Order of different parameter types | Yes | The type expected at a position changes |
| Parameter identifiers alone | No | The ordered type lists remain the same |
| Constructor body alone | No | The body is not part of the signature |
Reordering parameters only creates a different signature if the resulting ordered type list changes. Swapping identifiers whose declared types are the same leaves the type sequence unchanged. This is why comparing names alone gives an unreliable answer.
How should an invocation be matched?
Read the argument list at the creation expression. Check the number of arguments and the type of each argument in its position. Then examine the available constructor declarations and the types they accept.
The compiler resolves an overloaded invocation using the argument types and applicable language rules. A type conversion changes a value's representation or type as permitted by Java. Some permitted conversions mean that argument and parameter types need not have identical written names.
For straightforward calls with exactly matching types, match the ordered argument types to the declaration. When conversions are involved, check applicability and which constructor is most specific. Do not assume that matching the argument count alone is sufficient.
An ambiguous invocation is one for which overload resolution cannot select a unique most-specific applicable constructor. Such an invocation is rejected by the compiler. A call with no applicable accessible constructor also fails to compile.
What benefit does overloading provide?
Overloading keeps alternative initialisation routes within the same class. The creating code chooses a route through its arguments. It does not rename the class, and it does not cause every overloaded constructor body to execute merely because several are declared.
Keep the selection step separate from the execution step. First determine which declaration the call uses. Then trace that constructor's instructions. Reading every constructor body consecutively would produce an incorrect account of the object's starting state.
How does a constructor differ from a method?
Constructors and methods both have declarations and bodies, and both can receive parameters. These similarities make careful identification necessary. The decisive checks are the declaration's name, its return-type position, and its role in the program.
A method call invokes an operation declared by a method. An ordinary instance method operates with respect to an existing object. A constructor participates in initialising a new object during construction.
Which features should be compared?
| Feature | Constructor | Ordinary method |
|---|---|---|
| Purpose | Initialises an object during construction | Performs a declared operation |
| Name | Must match its class name | Need not match its class name |
| Return-type declaration | Has none, including no void | Declares a return type or void |
| Usual invocation considered here | Selected through an object-creation expression | Invoked through a method call |
| Automatic provision | A default constructor is supplied if none is declared | No corresponding automatic provision of a user-written operation |
| Overloading | Uses different constructor parameter lists | Uses the same method name with different parameter lists |
The last row identifies a similarity in the overloading mechanism, while the preceding rows distinguish constructors from methods. Do not present overloading itself as something that constructors can do but methods cannot.
Why is the return-type check useful?
A method may legally have the same name as its class. If its declaration includes a return type, including void, the declaration is a method. Matching the class name without checking the rest of the declaration is therefore insufficient.
Adding void to an intended constructor changes what has been declared. The resulting method is not invoked automatically as the constructor for a new expression. An initialisation instruction inside that method will require the method to be invoked.
No return type does not mean that the new expression has no value. The expression produces an object reference when construction succeeds. That expression result must be distinguished from a return type written on a method declaration.
A constructor can receive parameters without becoming a method. Equally, a method can receive no parameters without becoming a default constructor. Identify each declaration by its syntax and purpose rather than by a single superficial resemblance.
How can constructor questions be traced and checked systematically?
A trace follows the effect of program statements in execution order. Constructor tracing requires attention to the declarations, the object being created, the supplied arguments and the resulting field values. It also requires checking whether the program can compile.
What information should be recorded first?
List the instance fields and their types, together with any explicit initialisers. Separately list the constructors and their parameter types. Keeping fields separate from parameters prevents a receiving parameter from being mistaken for persistent object state.
- Check whether each intended constructor has the class name and no return type.
- Check whether the class declares constructors or instead qualifies for a compiler-supplied default constructor.
- At each creation expression, identify the arguments and the applicable accessible constructor.
- Associate arguments with parameters, then follow field assignments in execution order.
- Continue through subsequent method calls or output statements, preserving each object's separate instance values.
A compilation error is a problem detected before the program can execute successfully as written. A missing applicable constructor or a duplicate constructor signature is such a problem. Do not invent normal execution output for code that fails this check.
How can state and output be kept separate?
A field can change without being displayed. Likewise, a later output statement can reveal a value that was established during construction. Write down the state first, then use the actual output statements to determine what is displayed and in what order.
If a constructor leaves a field untouched, check its earlier initialisation before deciding its value. An explicit field initialiser may already have replaced the default. The absence of an assignment in the constructor body is not sufficient evidence of a default final value.
What should a final explanation make clear?
State why the selected constructor is applicable, how arguments reach parameters, and which assignments affect the object. If the task asks for a comparison, compare matching features. If it asks for an error, identify the broken rule and its consequence.
Variable description records a variable's name, type and purpose. For constructor work, its purpose should distinguish an instance field, a parameter and a reference variable. That distinction makes both program design and explanations easier to check.
Finally, make the conclusion depend on supplied information. A numerical final state needs the relevant starting values and assignments. A displayed result needs the output statements. Without that information, a general constructor rule cannot determine a particular program's output.
Glossary
- Class — A definition describing the data and operations associated with a kind of object.
- Object — An instance created from a class, with its own instance-variable storage.
- Constructor — A special class routine used during object creation to initialise the new object.
- Initialisation — Establishing the starting values or condition needed when an object begins its use.
- Instance variable — A field whose storage belongs separately to each object of the class.
- Default constructor — The no-argument constructor supplied by the compiler when a class declares no constructor.
- No-argument constructor — A constructor with an empty parameter list, which can also be explicitly declared.
- Parameterised constructor — A constructor whose declared parameters receive values supplied when that constructor is invoked.
- Argument — A value supplied in an invocation to be received by a corresponding parameter.
- Formal parameter — A receiving variable declared in a constructor or method's parameter list.
- Constructor overloading — Declaring constructors with different parameter lists within the same class definition.
- Constructor signature — The constructor name and ordered parameter types used to distinguish declarations here.
- Reference variable — A variable that can hold a reference to an object of a compatible type.
- Scope — The region of a program where a name can be used for its declaration.
- Method — A named routine that performs an operation and declares a return type or void.
Common errors and misconceptions
- Misconception: A constructor should declare void because it returns no value. Correct: A constructor declares no return type. Including void makes that declaration a method, even when its name matches the class.
- Misconception: Every class receives a compiler-supplied no-argument constructor. Correct: The compiler supplies a default constructor only when the class declares no constructors. Declaring a parameterised constructor prevents that automatic provision.
- Misconception: An explicitly written no-argument constructor is compiler-supplied. Correct: Its parameter list is empty, but the programmer wrote it. No-argument describes the list; default identifies the automatically supplied constructor.
- Misconception: Renaming parameters creates overloaded constructors. Correct: Overloads need different parameter-type lists. Changing identifiers while keeping the same number and ordered types produces duplicate signatures.
- Misconception: Declaring a reference variable creates an object. Correct: A declaration introduces the variable. In the construction form discussed here, the new expression creates the object and yields its reference.
- Misconception: A constructor parameter automatically becomes an instance field. Correct: The parameter receives an argument for that invocation. The constructor must perform the intended assignment to preserve that value in a field.
- Misconception: A field untouched by the constructor must retain its default value. Correct: An explicit field initialiser may already have supplied another value. Read the declaration as well as the constructor body.
- Misconception: All overloaded constructors run consecutively during ordinary object creation. Correct: The invocation selects an applicable constructor. Merely declaring alternatives does not execute all their bodies.
Exam-style questions with model answers
Q1. Define a constructor in Java and state its principal purpose. [2 marks]
- A constructor is a special routine declared in a class and invoked as part of creating an object.
- Its principal purpose is to initialise the new object by establishing its starting state through the required initialisation instructions.
Q2. State three characteristics that identify a Java constructor declaration: its name, return-type position and parameter list. [3 marks]
- The constructor's name must match the enclosing class name exactly. This connects the constructor declaration with the class whose objects it initialises.
- The declaration has no return type, including no void. A declaration containing a return type is a method rather than a constructor.
- Its parameter list is enclosed in parentheses and can be empty or contain declarations of parameters that receive supplied argument values.
Q3. Explain when Java supplies a default constructor, whether it takes arguments, and what happens to automatic provision when the programmer declares a parameterised constructor. [3 marks]
- The compiler supplies a default constructor when a class contains no constructor declarations. Declared fields and ordinary methods do not prevent this provision.
- The compiler-supplied default constructor takes no arguments. Its parameter list is empty, so it does not receive values through constructor arguments.
- Declaring a parameterised constructor prevents automatic provision of the default constructor. A required no-argument constructor must then be explicitly declared by the programmer.
Q4. Compare an explicitly declared no-argument constructor with a parameterised constructor using parameter lists, invocation arguments, initialisation and automatic default-constructor provision. [4 marks]
- The explicitly declared no-argument constructor has an empty formal parameter list. The parameterised constructor declares parameters to receive values.
- The no-argument form is invoked without arguments. The parameterised form requires arguments compatible with its declared parameters in their positions.
- The no-argument body can initialise fields through its written instructions. The parameterised body can also use values received through parameters.
- Both are programmer-declared constructors. Declaring either prevents the compiler from supplying a default constructor for that class.
Q5. Define constructor overloading. Explain how parameter number, parameter types and the order of different types can distinguish overloads, and why changing only parameter identifiers cannot. [5 marks]
- Constructor overloading means declaring constructors within the same class with different parameter lists. Their common name remains the name of that class.
- Changing the number of declared parameters produces a different parameter list. The argument count is consequently a necessary check when matching an invocation.
- Changing a parameter's declared type can distinguish an overload because the ordered list of types accepted by the constructor is then different.
- Changing the order of different parameter types can distinguish constructors. The type required at a particular position changes even when the total parameter count remains unchanged.
- Changing only parameter identifiers leaves the signature unchanged. Declarations with the same constructor name and ordered parameter types cannot be distinguished by renaming their receiving variables.
Q6. Compare a constructor with an ordinary instance method in Java under six headings: purpose, name, return-type declaration, usual invocation, automatic provision and overloading. Identify any shared feature. [6 marks]
- Purpose: A constructor initialises an object during construction, whereas an ordinary instance method performs its declared operation with respect to an existing object.
- Name: The constructor must have the class name. An ordinary method need not have that name, although Java permits it to do so.
- Return type: A constructor has no return-type declaration. A method declares a return type, or void when it does not return a value.
- Invocation: In ordinary object creation, a new expression selects the constructor. An ordinary instance method is invoked through a method call.
- Automatic provision: Java supplies a default constructor when no constructor is declared. There is no corresponding automatic provision of a user-written operation as a method.
- Overloading: Both can be overloaded using different parameter lists. This is a shared feature, with overloaded methods sharing their method name and constructors sharing the class name.
Q7. Consider instance fields of types int, double, boolean and a reference type. Assume no explicit field initialisers and no assignments changing them during construction. State each field's value after successful construction, identifying what each value means. [4 marks]
- The int instance field contains 0, meaning integer zero. No supplied initialiser or constructor assignment replaces this default value.
- The double instance field contains 0.0, meaning floating-point zero. It retains that value under the conditions stated in the question.
- The boolean instance field contains false, the false truth value. It is a logical value rather than a displayed numerical answer.
- The reference-type instance field contains null, which denotes no object reference. This does not mean that an object has been created for that field.
Q8. Explain a five-stage procedure for tracing a supplied Java program that creates objects with constructors. Cover declarations, constructor availability, argument matching, field changes and displayed output. [5 marks]
- Read the class declarations and identify instance fields, their types and explicit initialisers. Separately record constructor declarations so that fields are not confused with parameters.
- Check constructor availability. Determine whether the class declares constructors or qualifies for a compiler-supplied default constructor, and verify that the required declaration is accessible.
- At the object-creation expression, match the ordered argument types to an applicable constructor. Associate supplied values with the corresponding formal parameters before tracing its body.
- Follow initialisation and constructor assignments in execution order. Record changes to the new object's instance fields, taking account of explicit initialisers and any applicable default values.
- Continue through later statements and record output only where the program displays information. Keep displayed output separate from stored state, and do not predict normal execution for compilation errors.
Key takeaways
- A constructor initialises a new object and has the same name as its class, with no declared return type.
- The compiler supplies a default constructor only when the class declares no constructors of its own.
- An explicitly declared no-argument constructor can contain initialisation instructions even though its parameter list is empty.
- A parameterised constructor receives arguments through formal parameters, which must be distinguished from the object's instance fields.
- Constructor overloading requires different parameter lists; changing parameter identifiers alone does not create a different signature.
- Default field values and compiler-supplied default constructors are separate rules, so explicit constructors do not remove field defaults.
- The new expression creates an object and yields a reference; declaring a reference variable alone does not create that object.
- Trace the applicable constructor and actual field assignments before following output statements; object creation need not display anything.
Test yourself
Why must a constructor declaration omit void?
Void is a method return-type declaration. A constructor has no return type, so including void makes the declaration a method.
What condition allows the compiler to supply a default constructor?
The class must declare no constructors of its own. Declaring any constructor prevents automatic default-constructor provision.
Can an explicitly declared no-argument constructor initialise fields?
Yes. Its parameter list is empty, but its body can contain instructions that establish the object's initial field values.
Does changing only parameter identifiers create valid constructor overloads?
No. The constructor name and ordered parameter types remain the same, so renaming parameters does not distinguish the signatures.
When does changing parameter order distinguish overloaded constructors?
It distinguishes them when the ordered sequence of parameter types changes. Swapping names of same-typed parameters leaves that sequence unchanged.
Why does a field untouched by a constructor not necessarily hold its default value?
An explicit initialiser in the field declaration may already have assigned another value before the constructor body is followed.
How does this help when a constructor parameter and field have the same name?
This refers to the current object. Following it with a dot and the field name identifies the instance field rather than the same-named parameter.
Why is stored state insufficient to predict displayed output?
Field values describe state. Displayed output depends on the program's actual output instructions and their execution order.
