About Question com.enthuware.ets.scjp.v6.2.707 :

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

Moderator: admin

Post Reply
ETS User

About Question com.enthuware.ets.scjp.v6.2.707 :

Post by ETS User »

This is not correctly worded. For example:

Code: Select all

class Animal {
public void doMethodOfExtendStaticInnerClass(){}
}
public class Outer {
static Animal a = new Animal(){
public void doMethodOfExtendStaticInnerClass(){
//override me
}
}
public static void main(String[] args) {
Outer.a.doMethodOfExtendStaticInnerClass();
}
}
actually compiles correctly

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

Re: About Question com.enthuware.ets.scjp.v6.2.707 :

Post by admin »

I am not sure I understand your point. Can you please specify which option is not worded correctly so that we can investigate further?

thank you,
Paul.
If you like our products and services, please help us by posting your review here.

TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.707 :

Post by TwistedLizard »

From the commentary:
Q: Anonymous inner classes can never have initialization parameters.
A: They can if they are for classes.
ok, I get that:

Code: Select all

class Test0{
  static class Animal{
    String noise;
    Animal(String noise){this.noise=noise;}
    void makeNoise(){
      System.out.println(noise);
    };
  }
  public static void main(String[] args){
    //instantiate an anonymous sub-class of Animal with an initialization parameter
    new Animal("Woof!"){
      //override makeNoise
      public void makeNoise(){
        for(int i=0; i<3; i++)
          super.makeNoise();
      }
    }.makeNoise();
  }
}
output:

Code: Select all

>Test0
Woof!
Woof!
Woof!
but the commentary implies that an anonymous interface implementation cannot have initialization parameters. Here, an anonymous implementation of the CanMakeNoise interface is instantiated.

Code: Select all

class Test1{
  interface CanMakeNoise{
    void makeNoise();
  }
  public static void main(String[] args){
    new CanMakeNoise(){
      String noise;
      {
        noise = "Woof!";  //Is "Woof!" classified as an initialization parameter?
      }
      public void makeNoise(){System.out.println(noise);}
    }.makeNoise();
  }
}
output:

Code: Select all

>Test1
Woof!
is the literal assigned to noise, within the init block not regarded as as initialization parameter?

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

Re: About Question com.enthuware.ets.scjp.v6.2.707 :

Post by admin »

Not really. Initialization "parameter" would be something that is already defined and you change it by passing a different value. In your example, you are creating a new instance field and an instance initializer with a default value. You are not passing any parameter to the initializer.
If you like our products and services, please help us by posting your review here.

TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.707 :

Post by TwistedLizard »

Thanks.

Thought I recalled somewhere it being said that a value assigned inside an init block was regarded as an 'initialization parameter' . Your answer makes more sense.

marcioggs
Posts: 10
Joined: Sat Aug 31, 2019 3:19 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.707 :

Post by marcioggs »

A non static inner class may have static members.
Additional detail on the answer: If you make them final.

Could you please provide an example that compiles?
The one below doesn't.

Code: Select all

public class OuterClass {
    public class InnerClass {
        // Compilation error: Inner class cannot have static declarations.
        static final Object field = new Object();

        // Same error.
        static final void method() {}
    }
}

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

Re: About Question com.enthuware.ets.scjp.v6.2.707 :

Post by admin »

Code: Select all

class OuterClass {
    public class InnerClass {
        static final int field = 10; //compiles fine
    }
}

But I agree that the explanation should be improved to say that it is allowed only for constant variable declarations.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 45 guests