Java Programming - Declarations and Access Control - Discussion

4. 

Which three statements are true?

  1. The default constructor initialises method variables.
  2. The default constructor has the same access as its class.
  3. The default constructor invokes the no-arg constructor of the superclass.
  4. If a class lacks a no-arg constructor, the compiler always creates a default constructor.
  5. The compiler creates a default constructor only when there are no other constructors for the class.

[A]. 1, 2 and 4
[B]. 2, 3 and 5
[C]. 3, 4 and 5
[D]. 1, 2 and 3

Answer: Option B

Explanation:

(2) sounds correct as in the example below

class CoffeeCup { 
    private int innerCoffee; 
    public CoffeeCup() { 
   } 
  
    public void add(int amount) { 
    innerCoffee += amount; 
   } 
   //... 
 } 
The compiler gives default constructors the same access level as their class. In the example above, class CoffeeCup is public, so the default constructor is public. If CoffeeCup had been given package access, the default constructor would be given package access as well.

(3) is correct. The Java compiler generates at least one instance initialisation method for every class it compiles. In the Java class file, the instance initialisation method is named "<init>." For each constructor in the source code of a class, the Java compiler generates one <init>() method. If the class declares no constructors explicitly, the compiler generates a default no-arg constructor that just invokes the superclass's no-arg constructor. As with any other constructor, the compiler creates an <init>() method in the class file that corresponds to this default constructor.

(5) is correct. The compiler creates a default constructor if you do not declare any constructors in your class.


Stinger said: (Mar 18, 2011)  
In the above explanation, the class has default access but the constructor is public?

Tiago said: (Jan 19, 2012)  
In the example above, class CoffeeCup is public, so the default constructor is public. If CoffeeCup had been given package access, the default constructor would be given package access as well.

This is wrong. CoffeeCup on the example is default access.

Iroshan said: (Jun 10, 2014)  
Default constructor can be private as well.

Ankit said: (Jun 24, 2014)  
Please explain me this :

5. The compiler creates a default constructor only when there are no other constructors for the class.

class A{
int a;
int b;
int c;

A(int b,int c){

this.b=b;
this.c=c; }}

class B{
public static void main(String args [ ]){
A obj = new A(10,20);
System.out.println("a="+obj.a+" b="+obj.b+" c="+obj.c);

}
}

Output:a=0 b=10 c=20

How does variable a is assigned a value 0 if there is no default constructor made by the compiler ?

Pavani said: (Mar 5, 2015)  
Because 0 is default value for integer. Whenever object is created then memory allocated to instance variables (if there is no initialization for those variables then it takes default values) at the same time constructor also called.

so B and C gets directly 10 and 20 and there is no value for A, so it gets default value.

Prajwal said: (Nov 14, 2018)  
5) is correct. The compiler creates a default constructor if you do not declare any constructors in your class.

5) The compiler creates a default constructor only when there are no other constructors for the class.

Only is the difference. The default constructor is created when there is no param constructor defined.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.