开发者社区> 问答> 正文

java中非static方法内部能否嵌套非static方法?

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 个错误

展开
收起
蛮大人123 2016-06-13 14:00:55 2047 0
2 条回答
写回答
取消 提交回答
  • void function()
    {
    void show()
    {
    你这里语法就错了吧,怎么执行

    2019-07-17 19:35:48
    赞同 展开评论 打赏
  • 我说我不帅他们就打我,还说我虚伪

    语法是有问题的,一个方法可以发起对另外一个方法的调用,但是一个方法不能嵌套一个方法,这个和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方法的形式就是错误的。

    2019-07-17 19:35:48
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载