Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about anonymous inner class.

 
Ling Mike
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
----------------------------
public class Outer{

abstract class Food{
abstract void taste();
}

public static void main(String[] args){
Outer.Food eat = new Outer().new Food(){
public void taste(){
System.out.println("Very delicious.");
}
};
eat.taste();
}

}
---------
OK. It's good compiling and running, but if changing the class Food to an interface as following:
---------
public class Outer{

interface eatable{
void taste();
}

public static void main(String[] args){
Outer.eatable eat = new Outer().new eatable(){
public void taste(){
System.out.println("Very delicious.");
}
};
eat.taste();
}

}
-------
Now, this can not be compiled.
Who can tell me why?
Thanks very much!

BTW: I install the JDK5.0 in Japanese OS, so when I compile or run the java program, the error/warning messages is Japanese. How can I change the messages to English?
Thanks very much.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

public class Outer{

interface eatable{
void taste();
}

public static void main(String[] args){
Outer.eatable eat = new eatable(){
public void taste(){
System.out.println("Very delicious.");
}
};
eat.taste();
}

}
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember correctly, "inner interfaces" are static -- which means that it is closer to an abstract nested class, than an abstract inner class. This also means the correct syntax for declaring one should be...



Henry
 
Ling Mike
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
The "inner interface" as if is really static.

Now who can help me and answer my second question: How could I change the error/warning messages into English?
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic