java 不能在main方法中声明方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7470226/
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-30 20:07:06  来源:igfitidea点击:

can no methods be declared in the main method?

java

提问by Iam APseudo-Intellectual

I wrote this class (to print the array list) and in it I made a method in the mainfunction..

我写了这个类(打印数组列表),并在其中我在main函数中创建了一个方法..

Well it didn't work: I made the function staticand declared it in the mainmethod, still it didn't work. In the main method I made the function without an access specifier: still it didn't work.

好吧,它不起作用:我创建了函数static并在main方法中声明了它,但它仍然不起作用。在 main 方法中,我创建了没有访问说明符的函数:仍然没有工作。

Can't you declare a method in the mainmethod?

不能在方法中声明一个main方法吗?

Isn't there any way to declare a method inside the main method (other than making it public static outside the main method)?

没有任何方法可以在 main 方法中声明一个方法(除了在 main 方法之外将其设为 public static )?

public class TestArrays {


    public static void main(String[] args) {
    // Step 1 & 2: declare/initialize array variables
    int[]  array1 = { 2, 3, 5, 7, 11, 13, 17, 19 };
    int[]  array2;

    // Step 3: display array1 with initial values
        System.out.print("array1 is ");
    printArray(array1);
    System.out.println();

    // Step 4: make array2 refer to array1
    array2 = array1;
    // modify array2
    array2[0] = 0;
    array2[2] = 2;
    array2[4] = 4;
    array2[6] = 6;
    // print array 1
    System.out.print("array1 is ");
    printArray(array1);
    System.out.println();


     static void printArray(int[] array) {
    System.out.print('<');
    for (int i:array ) {
      // print an element
      System.out.print(i);
      // print a comma delimiter if not the last element

      }
       System.out.print('>');
    }
  }

回答by Joachim Sauer

No, you can't (directly*) defined methods inside other methods in Java (and the mainmethod is no special case here).

不,您不能(直接*)在 Java 中的其他方法中定义方法(main这里的方法不是特例)。

What you wantto do is to put the method in the same classas main. If mainneeds to call it without creating an instance of the class, then it needs to be static, but it need not be public.

你有什么想要做的是把方法相同main。如果main需要在不创建类的实例的情况下调用它,则它需要是static,但不一定是public

* You can declare a anonymous inner class with methods inside another method, however.

* 但是,您可以使用另一个方法中的方法声明匿名内部类。

回答by maialithar

you can't do this in java. java uses classes, they encapsulate everything, even the main method - you put methods in classes.

你不能在java中做到这一点。java使用类,它们封装了所有东西,甚至是主要的方法——你把方法放在类中。

回答by Biswadeb Panda

Try this code i think. it may work.

试试我认为的这段代码。它可能工作。

package practice;

class MyThreads extends Thread 

{
    MyThreads() 
    {
        System.out.print(" MyThreads");
    }
    public void run() 
    {
        System.out.print(" bar");
    }
    public void run(String s) 
    {
        System.out.println(" baz");
    }
}
public class TestThread 
{
    public static void main (String [] args) 
    {
        Thread t = new MyThreads() 
        {
            public void run() 
            {
                System.out.println(" foo");
            }
        };
        t.start();
        System.out.println("out of run");
    }
}

回答by Sai Pavan

Any features defined with in the method/block can't be accessed outside of it.

在方法/块中定义的任何功能都无法在其外部访问。

Because,As we know the scope of the that features ends at the end of the block/method.

因为,正如我们所知,该功能的范围在块/方法的末尾结束。

So there is no external(Outside the Method) use of it.

所以没有外部(方法之外)使用它。

for eg:

例如:

class Demo

{

       public static void main(String args[])

       {

       for(int i=0;i<10;i++) //for block

       {

           //.....

       }

       //outside of for loop

       System.out.println(i); // i can't be find symbol

       int sum()

       {

           int k=5;

       }

       //outside of sum()

       System.out.println(k); //won't get it.
   }

}

I think it is correct.otherwise correct me.

我认为这是正确的。否则纠正我。