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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

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: 10384
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.

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: 10384
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.

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: 10384
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.

jpgpsantos
Posts: 1
Joined: Thu May 30, 2024 8:49 am
Contact:

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

Post by jpgpsantos »

"Inner classes can't have static methods though" -> is this still the case in java 17?


It seems to contradict what is written in the explanation to option 1:

Since Java 16, non-static nested class i.e. inner class is allowed to have static members. Before Java 16, they were allowed to have a static field only if that field is final.
class OuterClass {     
public class InnerClass {        
static int VAL = 10; //COMPILES FINE        
static String STR = "1234"; //COMPILES FINE        
static Object obj = new Object(); /        
static int val2 = 10; //COMPILES FINE        
static final void method() {} //COMPILES FINE    
 }
}

Also, im on java 17, and its seems that inner calsses can have statis members, i.e., fields and methods, it compiles and runs ok

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

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

Post by admin »

Since Java 16 an inner class is allowed to have static members. That is why option 1 is marked correct. But yes, the statement in the explanation should be fixed.
thank you for your feedback!

AlienFS
Posts: 9
Joined: Tue Sep 05, 2023 4:07 am
Contact:

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

Post by AlienFS »

In the answer you wrote this:

Code: Select all

If anonymous class is created for interface, it extends Object class and implement that interface, if it is created for a class then it extends that class. Since Java 16, inner classes are allowed to has static fields as well as static methods. Example:
public class Outer
{
   class Inner
   {
     static int k = 10; //valid since Java 16
     static void m(); //valid since Java 16
   }
}
How can static method m() be valid if it has no body implementation?

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

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

Post by admin »

You are right. { } at the end is missing. Fixed.
thank you for your feedback!

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests