allenharveyiii
allenharveyiii

Reputation: 17

Illegal Start of Expression : declaring a method inside another method

I know this will probably be an easy fix, but i'm just starting out in java. I need to declare a method inside the main method that clears the screen. Line 5 is giving me an error called Illegal start of expression.

public class Project2
{
public static void main(String [] args)
{
    public static void clearScreen()
    {
    System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    }// end clearScreen()

System.out.print("\nDid it work?");
}
}   

Upvotes: 0

Views: 210

Answers (2)

Rahul
Rahul

Reputation: 45060

Nested methods is not allowed in Java(as of yet). The closest you can get is

class Project2 {
    public static void main(String [] args) {
        class InnerClass {
           void clearScreen() {
               // Do something.
           }
         }
         new InnerClass().clearScreen(); // Call it this way.
     }
 }

If the above solution doesn't suit, then just move that method outside your main and call it.

Upvotes: 1

StormeHawke
StormeHawke

Reputation: 6207

You can't put a method inside a method like that. You call methods from methods, like so:

public class Project2
{
   public static void main(String [] args)
   {
       clearScreen();

      System.out.print("\nDid it work?");
   }

public static void clearScreen()
{
    System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}// end clearScreen()
}   

Upvotes: 0

Related Questions