Java Programming - Declarations and Access Control - Discussion

3. 

What will be the output of the program?

public class A
{ 
    void A() /* Line 3 */
    {
        System.out.println("Class A"); 
    } 
    public static void main(String[] args) 
    { 
        new A(); 
    } 
}

[A]. Class A
[B]. Compilation fails.
[C]. An exception is thrown at line 3.
[D]. The code executes with no output.

Answer: Option D

Explanation:

Option D is correct. The specification at line 3 is for a method and not a constructor and this method is never called therefore there is no output. The constructor that is called is the default constructor.


Abhijit Saha said: (Sep 2, 2010)  
I have a doubt in Java. In c++, only the constructor name is the same as the class name. i.e., C++ does not allow any other member functions can have the name same as the class name and also the constructor returns no value.

But in Java, we can have a method name same as the class name even though it is not a constructor. i.e.,

class myClass
{
void myClass() // constructor
{
}
int myClass() // not a construcor, but java allows it , why?
{
}
}

Suhas said: (Feb 27, 2011)  
Constructor in java doesnot have return type

Sridevi.s said: (Aug 5, 2011)  
In Abhijit Saha's example, void myclass is a constructor to class myclass then,
Why void A is not a constructor to class A?

can anyone clarify with clear examples?

Amaz said: (Apr 24, 2012)  
void myClass is not constructor my dear friend,first of all we must know constructor are the speacial method in java which has the exact same name as the class,and must not have a return type,here in abhijeet's question void myClass() is not a constructor because its having return type that is void,so java compiler won't deal it as a constructor.

Muthu said: (Aug 17, 2013)  
In java Constructor doesn't have return type right. Then how is it possible?

public class A
{
void A() /* Line 3 */
{
System.out.println("Class A");
}

Gtrbt said: (Jan 21, 2014)  
Because constructor doesn't specify return type.

Sravan said: (Jun 19, 2014)  
A() is a method not a constructor. So, the anonymous object created is not calling method A(). The code will execute without any output.

Pramod Pai said: (Jul 12, 2014)  
Constructor can only initialize like:

void A(int a)
{
this.a=a;
}

Where as method will have statements:

void A
{
print...(bla bla bla);

}

Hope you're clear.

Dilly said: (Aug 12, 2014)  
Unlike C++ in Java we can have method name same as of class name.

But like C++ constructor doesn't have return type not even void.

Mahi said: (Sep 12, 2014)  
class a
{
void a(){}
}
a()
{}
}

is both of the methods are constructor of class a, if not why?

Sandeep Tomar said: (Oct 5, 2014)  
If we are allowed to have method with same name. How Compiler will come to know whether we have written a valid function code or a wrong constructor code. Please help to clarify on this. Thanks.

Aqueenni said: (Sep 14, 2015)  
As per my knowledge, method name starts with a lowercase.

Here it is void A().

So one can also think that it is a constructor declared wrongly and hence the option B. Compilation fails.

Ishu said: (Jul 27, 2016)  
Constructor don't have return type and in this problem there is return type which is void A() so this is a method and int his very question method name is also with same name as class so block will not execute.

Alexandre said: (Jan 14, 2017)  
For me, the answer is "B" Compilations fails, because the compiler by Eclipse gives me this message (before compile, I can't compile).

"This method has a constructor name".

Ayylmao said: (Jun 19, 2017)  
This is easy, get the constructor basics right first.

Like everyone said a constructor doesn't have a return type.
Therefore void A(){//code}, int A(){//code}, float A(){//code} ,etc. are all methods where as,
A(){//code} is a constructor

Therefore in the given code, just remove 'void' and i'll become a constructor and print "Class A" otherwise it won't do anything.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.