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.