Page 1 of 1
					
				About Question com.enthuware.ets.scjp.v6.2.707 :
				Posted: Wed Nov 30, 2011 11:49 pm
				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
 
			 
			
					
				Re: About Question com.enthuware.ets.scjp.v6.2.707 :
				Posted: Thu Dec 01, 2011 9:06 pm
				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.
			 
			
					
				Re: About Question com.enthuware.ets.scjp.v6.2.707 :
				Posted: Tue Jun 20, 2017 3:02 pm
				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:
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:
is the literal assigned to noise, within the init block not regarded as as initialization parameter?
 
			 
			
					
				Re: About Question com.enthuware.ets.scjp.v6.2.707 :
				Posted: Tue Jun 20, 2017 9:18 pm
				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.
			 
			
					
				Re: About Question com.enthuware.ets.scjp.v6.2.707 :
				Posted: Wed Jun 21, 2017 12:52 pm
				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.
			 
			
					
				Re: About Question com.enthuware.ets.scjp.v6.2.707 :
				Posted: Sun Apr 04, 2021 4:06 pm
				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() {}
    }
}
 
			 
			
					
				Re: About Question com.enthuware.ets.scjp.v6.2.707 :
				Posted: Mon Apr 05, 2021 12:18 am
				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.
 
			 
			
					
				Re: About Question com.enthuware.ets.scjp.v6.2.707 :
				Posted: Thu May 30, 2024 8:52 am
				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
			 
			
					
				Re: About Question com.enthuware.ets.scjp.v6.2.707 :
				Posted: Thu May 30, 2024 9:15 am
				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!
			 
			
					
				Re: About Question com.enthuware.ets.scjp.v6.2.707 :
				Posted: Wed Nov 06, 2024 10:50 am
				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?
 
			 
			
					
				Re: About Question com.enthuware.ets.scjp.v6.2.707 :
				Posted: Wed Nov 06, 2024 11:20 pm
				by admin
				You are right. { } at the end is missing. Fixed.
thank you for your feedback!