public class While {
    public static void main(String args[]){

    }
    public void printLoop(){

        int counter = 1;
        while(counter <=25){
            System.out.println(counter);
            counter++;
        }
         }
    }

Hi, the above code compiles but nothing runs when I click on the run button. I am new to Java, you guys might find this problem easy to solve. Thanks.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

when I click on the run button.

Where is the run button?
Are you having a problem with your IDE?
If so you should ask the question on a site for that IDE.

Try executing your program from a command prompt.Open a command prompt window, change to the folder with the While.class file and enter:
java While

It doesn't look like your main() method is doing anything. Is this an omission, or is this the actual code?
If this is the actual code, try putting a call to the printLoop method in the main method and see what happens.

(I assume from the presence of the main() method that this is intended to run as a standalone).

jon is right. You have to put the printLoop method into the main method. This means when interpreting the program (executing the main() method only) the printLoop() is called.
However, there are two ways to follow jon's instruction.
(1) you have to place a keyword static before the method printLoop(), so that the static method main may call the printLoop().
static void printLoop() {...}
static method can call static mathods only. Or
(2) the printLoop() remains as a member method. In this way you have to create an instance of the class "While" you have defiend. Then the instance will be used as a handle to call the printLoop().

public static void main(String args[]) {
While w= new While();
w.printLoop();
}

Edited 6 Years Ago by tong1: n/a

That's true, good you mentioned it. One of the most frustrating sources of confusion at the start of java programming, and it doesn't become clear why this rigamarole is necessary until you really look into what objects are and what classes are.

for now, either of the methods Tong suggests are good, or you can just make a constructor that has all the code you'd call in main():

private While w;

public static void main(String[] args)
{
  w = new While();
}

public While()
{
  printLoop();
  doSomethingElse();
  etc.
}

This transfers your code into the constructor, and you don't have to use the object reference all the time. Eventually when you're writing in a more OO style, this sort of stuff will go away, but for now it's fine.

private While w;

public static void main(String[] args)
{
  w = new While();
}

public While()
{
  printLoop();
  doSomethingElse();
  etc.
}

The Code that you wrote jon.kiparsky, will not compile. Please don't post code that you haven't tested.

Also it is better to learn to call methods after initializing an object through the constructor.
The code posted by tong1 is a very good advice:

public static void main(String args[]) {
        While w= new While();
        w.printLoop();
      }

Edited 6 Years Ago by javaAddict: n/a

JA - you're right. The declaration should have been in main, not as a class field. Trivial, but correct.

The point, of course, was that there are a few ways of getting around the issue of running methods out of the main() class, and that running them out of the constructor is one typical idiom for this. The only other one I can think of that might be mentioned here is the most annoying and arguably the most OO of the bunch. That would be to have a "PrintLoopMain" class with your main() method, and have that instantiate and call "PrintLoop". If you want to be pedantic, that's the pedant's choice.
But since we're not actually doing an object-oriented solution here it's silly to slap a coat of OO paint on it and pretend. Might as well admit that this is procedural Java and go on with it. In that case, running out of the constructor is the most inutitive way to handle this particular sort of code.

Hi it works! Thank you for helping. So Basically, I have to create an object and have to call the method in the static void main method.

public class while1 {

    public static void main(String args[]){
        while1 w= new while1();
        w.printLoop();

    }

    public void printLoop(){

    int index=0;
    while(index <5){
        System.out.println(index);
        index++;
    }
    }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.