class NiMingLei
{
public static void main(String[] args)
{
Outer a= new Outer();
a.function();
}
}
class Outer
{
static int y=4;
void function()
{
void show()
{
System.out.println(123);
}
class Inter
{
void method()
{
System.out.println(234);
}
}
new Inter().method();
}
}
NiMingLei.java:15: 错误: 非法的表达式开始
void show()
^
NiMingLei.java:15: 错误: 需要';'
void show()
^
2 个错误
语法是有问题的,一个方法可以发起对另外一个方法的调用,但是一个方法不能嵌套一个方法,这个和javascript是有区别的。
如
void methodA(){
System.out.println("我要调用B方法了");
methodB();
}
void methodB(){
System.out.println("我是B方法");
}
这样就是正确的。
如果:
void methodA(){
System.out.println("我要调用B方法了");
void methodB(){
System.out.println("我是B方法");
}
}
这样在A方法中嵌入B方法的形式就是错误的。
static 方法必须调用static的方法。非static方法中可以调用非static方法
你需求描述有问题,不能方法嵌方法,只有方法掉用方法的。
你可以这么写,感觉你对C系列语言不熟悉
class NiMingLei
{
_ /////////////////////////////////////// _ static int yy = 4;//也可以在这里声明,初始化;
public static void main(String[] args)
{
class Inter {
void method() {
System.out.println(234);
}
}
class Outer {
private final static int y = 4 ;
void function() {
new Inter().method();
}
void show() {
System.out.println(123);
}
}
Outer a = new Outer();
a.function();
}
}