Page 1 of 1
About Question enthuware.ocpjp.v11.2.3503 :
Posted: Tue Feb 16, 2021 10:35 pm
by mesginaa01
public class TestOuter { public static class TestInner {
public void sayIt(){ System.out.println("hello"); } }
public static void main(String[] args){
//call here
} }
Why TestInner.sayIt(); cannot be the the second answer?
Re: About Question enthuware.ocpjp.v11.2.3503 :
Posted: Wed Feb 17, 2021 1:22 am
by admin
As the explanation for this option says:
TestInner is static but its method sayIt is not. So, you need to do new TestInner().sayIt(); in main to invoke sayIt.
You can actually try it out.
Re: About Question enthuware.ocpjp.v11.2.3503 :
Posted: Wed Jan 25, 2023 3:06 am
by yuir12
Hello, question:
new TestOuter.TestInner() -> are we using this call because TestInner is static? Thanks
Re: About Question enthuware.ocpjp.v11.2.3503 :
Posted: Wed Jan 25, 2023 6:53 am
by admin
Yes, that is correct. Just like you refer to any other static member of a class.
Re: About Question enthuware.ocpjp.v11.2.3503 :
Posted: Sun Jan 28, 2024 7:41 am
by powerofviva
Hi,
Code: Select all
public class TestOuter
{
public static class TestInner
{
public void sayIt(){ System.out.println("hello"); }
}
public static void main(String[] args){
//call here
new TestOuter.TestInner() // ---> CLass or package is needed !!
}
public void tee() {
new TestOuter.TestInner() // ---> even here compiler error, CLass or package is needed !!
}
}
I am wondering how you did not get that error that I am getting! it even does not compile!
Re: About Question enthuware.ocpjp.v11.2.3503 :
Posted: Sun Jan 28, 2024 7:46 am
by powerofviva
even your last answer does not compile:
TestInner.sayIt(); can be inserted in main.
TestInner is static but its method sayIt is not. So, you need to do new TestInner().sayIt(); in main to invoke sayIt.
inside static main method:
new TestInner().sayIt(); ---> TestOuter.this can not be referenced from statis method. so new TestInner() wont compile!
you have to call is inside non static method.
Re: About Question enthuware.ocpjp.v11.2.3503 :
Posted: Sun Jan 28, 2024 11:40 am
by admin
The given answers and explanations are correct. Please read the options carefully.
BTW, you are missing a semi-colon in your code.
The last option indeed does not compile and that it why that option is an incorrect option.