About Question enthuware.ocpjp.v11.2.3503 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
mesginaa01
Posts: 1
Joined: Sat Aug 15, 2020 6:37 pm
Contact:

About Question enthuware.ocpjp.v11.2.3503 :

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3503 :

Post 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.
If you like our products and services, please help us by posting your review here.

yuir12
Posts: 22
Joined: Thu Dec 09, 2021 11:23 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3503 :

Post by yuir12 »

Hello, question:

new TestOuter.TestInner() -> are we using this call because TestInner is static? Thanks

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3503 :

Post by admin »

Yes, that is correct. Just like you refer to any other static member of a class.
If you like our products and services, please help us by posting your review here.

powerofviva
Posts: 8
Joined: Tue Sep 19, 2023 12:11 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3503 :

Post 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!

powerofviva
Posts: 8
Joined: Tue Sep 19, 2023 12:11 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3503 :

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3503 :

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 34 guests