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();
}
}
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.
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.
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?
{
void a(){}
}
a()
{}
}
is both of the methods are constructor of class a, if not why?
Sandeep Tomar said:
1 decade ago
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:
10 years ago
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.
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:
9 years ago
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers