public class TestClass
{
public class A
{
}
public static class B
{
}
public void useClasses()
{
//1
}
}
a. new TestClass().new A()
b. new TestClass.B();
c. new A();
d. new TestClass.A();
e. All of these are valid.
I marked option a by the statement "correctly instantiate objects of various classes". wrong interpretation ? Because the only one that make various object instances is in option a.
Why new TestClass.A(); should work here , it is a static nested inner class syntax but A is not a static ?
It is a bit confused that both below syntax works while B is static and A is not
The code new TestClass.A() is within an instance method of class TestClass. Therefore, there is already an instance of TestClass associated with the method (i.e. "this" ). That is why, even though A is not static, new TestClass.A() is valid inside the given method.
If you like our products and services, please help us by posting your review here.
new TestClass() is the syntax of creating a TestClass object. So new TestClass().A(); will be same as invoking a method named A on the newly created TestClass object. But you know that there is no method named A in TestClass.
If you like our products and services, please help us by posting your review here.
The text says that the option chosen must instantiate objects of various classes.
B, C and D instantiates objects of only one class, not various.
A is the only answer that instantiates objects of more than one class (TestClass and A).
The question text should be changed to something different not to be ambiguous, like:
"Which of the given options could be inserted at //1 in order to compile".
That´s what this question is about.