开发者

can no methods be declared in the main method? [duplicate]

开发者 https://www.devze.com 2023-04-06 08:37 出处:网络
This question already has answers here: Writing a function inside the main method - Java (7 answers) Closed 6 years ago.
This question already has answers here: Writing a function inside the main method - Java (7 answers) Closed 6 years ago.

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

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

Can't you declare a method in the main method?

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

public class TestArrays {


    public stat开发者_StackOverflow中文版ic 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('>');
    }
  }


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

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

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


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消