Java Programming - Declarations and Access Control - Discussion

Discussion Forum : Declarations and Access Control - Finding the output (Q.No. 3)
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(); 
    } 
}
Class A
Compilation fails.
An exception is thrown at line 3.
The code executes with no output.
Answer: Option
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.

Discussion:
15 comments Page 2 of 2.

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

Dilly said:   1 decade ago
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:   1 decade ago
class a
{
void a(){}
}
a()
{}
}

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

Gtrbt said:   1 decade ago
Because constructor doesn't specify return type.

Suhas said:   1 decade ago
Constructor in java doesnot have return type


Post your comments here:

Your comments will be displayed after verification.