Java Programming - Declarations and Access Control - Discussion


public class Test { }
What is the prototype of the default constructor?

[A]. Test( )
[B]. Test(void)
[C]. public Test( )
[D]. public Test(void)

Answer: Option C


Option A and B are wrong because they use the default access modifier and the access modifier for the class is public (remember, the default constructor has the same access modifier as the class).

Option D is wrong. The void makes the compiler think that this is a method specification - in fact if it were a method specification the compiler would spit it out.

Ashwin said: (Aug 18, 2011)  
Why the option A is wrong ?
The compiler will implicitly put "public" in front of Test(). Then why this option is wrong.

And wht do you mean by prototype ?

Mahesh said: (Jan 2, 2012)  
Option A is wrong. The default constructor has the same access modifier as the class. Here the is defined public so default constructor must be public. And Test() is in default access level.

The compiler will not implicitly put "public" in front of Test().

Saritha said: (Jan 31, 2012)  
If the class having the access modifier as private. Then is it the default constructor of class is private?

Sushil said: (Dec 16, 2013)  
//I have written simple program
public class Outer

System.out.println(" This less access specifier");
public static void main(String[] argv)
new Outer();

This less access specifier
/*This means option A is also correct */
If I am wrong please clarifiy me.

Mohan said: (Jul 19, 2014)  
What is the prototype of constructor in before creating an object?

Jothi Pandiyan said: (Apr 1, 2015)  

In your program you are creating an object and overriding default constructor. Try this, create object without constructor and compile it.

Here compiler creates an constructor with the access modifier same as class access modifier. To check this, use javap command in your command prompt.

//Your program:

public class Outer
public static void main(String[] argv)
{ new Outer();

//compile this source code as
-> javac
-> javap Outer

Harshini said: (Aug 5, 2016)  
@Jothi Pandiyan.

What is the meaning of this?

java Outer

Msheliga said: (Jan 28, 2017)  
Another way of looking at this is that you can overwrite the default constructor to be default or protected, (and likely even private). I've done this below.

However, if you do *NOT* overwrite the default constructor its public and therefore accessible from anywhere.

public class NewTest
public int i = 0;

// protected NewTest() // This compiles even though its protected (not public as class is).
NewTest() // This alsp compiles even though its default (not public as class is).
System.out.println("NewTest() new called.");
i = 99;

public static void main(String args[])
NewTest sub = new NewTest();

Pranay said: (Jul 7, 2017)  
Option A is correct.

And I agree @Jothi Pandiyan.

Shubhamsinh said: (Aug 24, 2019)  
I think Option A is correct.

Soumik Sarkar said: (Mar 9, 2021)  
In the above example, Outer () is a non-parameterised constructor. Not a default constructor. We can not initialize the default constructor. It will automatically initialize by the compiler itself when we can not define a constructor in a class.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.