【问题标题】:can no methods be declared in the main method? [duplicate]不能在main方法中声明方法吗? [复制]
【发布时间】:2011-11-20 04:16:00
【问题描述】:

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

它不起作用:我创建了函数 static 并在 main 方法中声明它,但它仍然不起作用。在 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('>');
    }
  }

【问题讨论】:

    标签: java


    【解决方案1】:

    在方法/块中定义的任何特性都不能在它之外访问。

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

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

    例如:

    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.
       }
    
    }
    

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

    【讨论】:

      【解决方案2】:

      试试我认为的这段代码。它可能会起作用。

      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");
          }
      }
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          不,您不能(直接*)在 Java 中的其他方法中定义方法(main 方法在这里没有特殊情况)。

          你想要做的是将方法放在与main相同的类中。如果main 需要在不创建类实例的情况下调用它,那么它需要是static,但它不需要是public。

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

          【讨论】:

            猜你喜欢
            • 2012-10-29
            • 2020-12-17
            • 2014-11-09
            • 1970-01-01
            • 2014-05-12
            • 2019-07-04
            • 2019-02-26
            • 1970-01-01
            • 2014-03-29
            相关资源
            最近更新 更多