1

I am trying to create a while loop, but the compiler keeps saying I have an "illegal start of type." How do I fix this?

code:

class whileLoop
{
   int p = 0;
   while(p < 10)
   {
       System.out.println(p);
       p++;
   }
}
3
  • 4
    Put those codes inside a main method. Commented Aug 8, 2013 at 18:43
  • Try to understand, how does the JVM execute a code? What is the starting point of the program? And do you have it in your code? Commented Aug 8, 2013 at 18:47
  • I highly suggest checking out a Java tutorial, e.g. docs.oracle.com/javase/tutorial Commented Aug 8, 2013 at 18:48

5 Answers 5

6

Put your code in a valid main method:

public static void main(String[] args) {
   // code here
}

In Java, your code must go inside a method, constructor or initialization block; it cannot simply reside in the class body. When you "run" a program, the main method (as shown above) is invoked.

Sign up to request clarification or add additional context in comments.

3 Comments

There is code that comes before this however. And that code is in a main method. Wouldn't creating another main method confuse the compiler?
@user2662047 You can only have one main method per class. You can have two if they're in different classes.
@user2662047 We've been telling you to put it inside a main method because we thought that the code you posted was all you had. If you already have a main method, put this code inside that same main method or some other method, depending on what you're actually trying to do.
2

You need to put this in some method or initialization block.

in an initialization block, it means the code will execute every time an instance of the class is created

    {
        int p = 0;
        while (p < 10) {
            System.out.println(p);
            p++;
        }
    }

in instance method, the code is executed whenever the method is invoked.

    public void someMethod() {
        int p = 0;
        while (p < 10) {
            System.out.println(p);
            p++;
        }
    }

in main method.

    public static void main(String[] args) {
        int p = 0;
        while (p < 10) {
            System.out.println(p);
            p++;
        }
    }

Comments

0

loop should be inside method like

 public static void main(String[] args){  
 int p = 0;
  while(p < 10)
  { 
   System.out.println(p);
   p++;
  }
 }

Comments

0

You can't just put arbitrary code directly into the body of a class. You need put it in a method (or an initializer block, a slightly more advanced topic), e.g.

class whileLoop
{
   public static final void main (String[] args) {
     int p = 0;
     while(p < 10)
     {
       System.out.println(p);
       p++;
     }
   }
}

The main method is a special method that is called when you invoke your class through the java command line. You can define any other methods that you need to, though. However, I highly suggest checking out a basic Java tutorial, e.g. http://docs.oracle.com/javase/tutorial/

Comments

0

You can't just put this in the class, it must be part of a method inside the class:

class whileLoop
{
    public static void main(String[] args) {
        int p = 0;
        while(p < 10)
        {
            System.out.println(p);
            p++;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.