Results 1 to 3 of 3
  1. #1
    phantomcraft is offline Member
    Join Date
    Feb 2013
    Location
    The Netherlands
    Posts
    17
    Rep Power
    0

    Default Main method won't work?

    Java Code:
    package com.limepop.client;
    
    public class main {
    	
    	
    	
    	// Variables
    	static boolean isRunning = false;
    	//
    	
    	 static void print (String s){
    	        System.out.println(s);
    	        }
    	
    	public static void gameLoop(){
    		while(isRunning){
    			print("Running...");
    		}
    	}
    	
    	public static void main(String[] args) {
            gameLoop();
        }
    	
    	
    }
    The main method says "This method has a constructor name"

    My class is also called main if that matters.

    Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Main method won't work?

    Quote Originally Posted by phantomcraft View Post
    Java Code:
    package com.limepop.client;
    
    public class main {
    	
    	
    	
    	// Variables
    	static boolean isRunning = false;
    	//
    	
    	 static void print (String s){
    	        System.out.println(s);
    	        }
    	
    	public static void gameLoop(){
    		while(isRunning){
    			print("Running...");
    		}
    	}
    	
    	public static void main(String[] args) {
            gameLoop();
        }
    	
    	
    }
    The main method says "This method has a constructor name"

    My class is also called main if that matters.

    Thanks
    That is just a compiler warning; while it is perfectly legal to name a method the same as the class name, it is silly and confusing, hence the warning; don't worry, your main method runs fine but the isRunning variable is false so nothing is printed. You have to fix that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Main method won't work?

    First, you shouldn't call your class main. It will work but it's not a good idea (I assume the constructor message was a warning). And class names should be capitalized by convention. Second, you don't need an instance of the class to invoke a static method so gameloop() should be just fine. But change the boolean to true or the loop will never run.

    Regards,
    Jim
    Last edited by jim829; 05-15-2013 at 07:05 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 4
    Last Post: 03-28-2013, 08:40 PM
  2. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 08:30 AM
  3. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 11:03 AM
  4. Replies: 6
    Last Post: 12-23-2009, 09:10 AM
  5. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •