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 1 of 2.

Alexandre said:   9 years ago
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".
(1)

Ayylmao said:   8 years ago
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.
(1)

Abhijit Saha said:   1 decade ago
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:   1 decade ago
Constructor in java doesnot have return type

Sridevi.s said:   1 decade ago
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:   1 decade ago
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:   1 decade ago
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:   1 decade ago
Because constructor doesn't specify return type.

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.

Pramod pai said:   1 decade ago
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.


Post your comments here:

Your comments will be displayed after verification.