?
What is the output of the program?
What is the output of the program?
class MyClass{
MyClass(){
System.out.print("one");
}
public void myMethod(){
this();
System.out.print("two");
}
}
public class TestClass{
public static void main(String args[]){
MyClass obj = new MyClass();
obj.myMethod();
}
}
class MyClass{
MyClass(){
System.out.print("one");
}
public void myMethod(){
this();
System.out.print("two");
}
}
public class TestClass{
public static void main(String args[]){
MyClass obj = new MyClass();
obj.myMethod();
}
}
Answer & Solution
Correct Answer:
Option
D
The code will result in a compilation error because in the method myMethod, the use of this() is incorrect.
In Java, this() is used to call another constructor, but it can only be used in a constructor, not in a method.
Using this() in myMethod causes a compilation error.
Thus, the program will not compile, and the correct answer is a Compilation Error.
In Java, this() is used to call another constructor, but it can only be used in a constructor, not in a method.
Using this() in myMethod causes a compilation error.
Thus, the program will not compile, and the correct answer is a Compilation Error.
Join the Discussion
Login to post a comment or share your explanation.
Login