java 为什么我们不能在Java的main方法中实现一个方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12547488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:19:59  来源:igfitidea点击:

Why can't we implement a method inside the main method in Java?

java

提问by R Syed

Why doesn't this work?

为什么这不起作用?

public class AddArray
{
    public static void main(String[] args) 
    {

        int[] x = {1,2,3};
        int[] y = {1,2,3};

        dd(x,y);

        public static void add(int[]a, int[]b)
        {
            int[] sum = new int[a.length];
            for (int i=0; i<a.length; i++)
                sum[i] = a[i] + b[i];
            for (int i=0; i<a.length; i++)
                System.out.println(sum[i]);
        }
    }
}

回答by assylias

You can't define a method within another method in Java. In particular, you can't define a method within the mainmethod.

您不能在 Java 中的另一个方法中定义一个方法。特别是,您不能在方法中定义main方法。

In your case, you could write:

在你的情况下,你可以写:

public class AddArray {

    public static void main(String[] args) {

        int[] x = {1,2,3};
        int[] y = {1,2,3};

        add (x,y);
    }

    private static void add (int[] a, int[] b) {
        int[] sum = new int[a.length];
        for (int i = 0; i < a.length; i++)
            sum[i] = a[i] + b[i];
        for (int i = 0; i < a.length; i++)
            System.out.println(sum[i]);
    }
}

回答by Rohit Jain

Well, Java does not support nested function. But the question is why would you need that?? If you really have a situation where you want a nested method, then you can go with a local class.

好吧,Java 不支持嵌套函数。但问题是你为什么需要那个?如果您确实遇到需要嵌套方法的情况,那么您可以使用local class.

It looks like this: -

它看起来像这样:-

public class Outer {
    public void methodA() {
         int someVar = 5;

         class LocalClass {
              public void methodB() {
                   /* This can satisfy your need of nested method */
              }
         }

         // You cannot do this instantiation before the declaration of class
         // This is due to sequential execution of your method..

         LocalClass lclassOb = new LocalClass();
         lclassOb.methodB();
    }
}

However, you must note that, your local class will be visible only in the scope it is defined. It cannot have modifier: private, public, or static.

但是,您必须注意,您的本地类仅在它定义的范围内可见。它不能有修饰符:privatepublicstatic

回答by Reimeus

Methods cannot be defined inside other methods in Java. For this to compile your addmethod would have to be extracted out of the mainmethod.

不能在 Java 中的其他方法中定义方法。为了编译你的add方法,必须从main方法中提取出来。

回答by Thorbj?rn Ravn Andersen

Because the Java Language Specification does not allow it.

因为 Java 语言规范不允许这样做。

Methods belong directly under a class, and cannot be nested.

方法直接属于一个类,不能嵌套。

回答by Hymancogdill

In java you have to keep methods separate. :/ sorry

在 Java 中,您必须将方法分开。:/ 对不起