Results 1 to 11 of 11
  1. #1
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default compiler telling me to make a file for a public class

    Hi, any help greatly appreciated. The compiler is telling me that since two of my classes are "public", that I need to make separate files for them, which I know should not be the case right? So I pasted the code below so you can see it. and after that I posted the error. please. thank you gurus! Derekconfused:

    Java Code:
    /////////////////////////////////////////////////////////////////////////////////////
    
    public class Player {
    	int number = 0; //where the guess goes
    	
    	public void guess() {
    		number = (int) (Math.random() * 10);
    		System.out.println("I'm guessing " + number);
    	}//end guess
    }//end player class
    
    ///////////////////////////////////////////////////////////////////////////////////////
    
    public class GameLauncher {
    	public static void main (String[] args) {
    		GuessGame game = new GuessGame();
    		game.startGame();
    	}
    }



    Java Code:
    Class Player is public should be declared in a file named Player.java
    
    Class GameLauncher is public should be declared in a file named GameLauncher.java
    On a side note! I would like to share a great trick with fellow newbie programmers, and when I say newbie, I mean first born. LOL. Ok, you select your compiler errors or type them, then google them in quotes, and search for the solution. If that doesn't work, try the forum, that's what I did.
    Last edited by silverglade; 04-26-2011 at 12:07 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by silverglade View Post
    Hi, any help greatly appreciated. The compiler is telling me that since two of my classes are "public", that I need to make separate files for them, which I know should not be the case right?
    The compiler is correct (as per usual). A public class needs to be in its own file.

  3. #3
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Thank you! Yes I don't mess with the compiler, he is uber. lol. This is really strange. I am not used to that. So what do I do if I have a ton of classes in the same app, and want to just have one file for them , if it's a small app. I really would not like to make separate files for the classes, Here is my full code, please let me know how I should approach this. Like what should be in separate files and what should not, and if there is an alternative to naming your class public just so you can keep it in the same main file. Ok any more help greatly appreciated. Thank you. Derek EDIT: if I have to compile more than one file, I don't know how to do that.

    Java Code:
    
    /* guessgame by my Head First Java book pg 39-40*/
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    public class GuessGame {
    
    	Player p1;
    	Player p2;
    	Player p3;
    	
    	public void startGame() {
    		p1 = new Player();
    		p2 = new Player();
    		p3 = new Player();
    		
    		int guessp1 = 0;
    		int guessp2 = 0;
    		int guessp3 = 0;
    		
    		boolean p1isRight = false;
    		boolean p2isRight = false;
    		boolean p3isRight = false;
    		
    		int targetNumber = (int) (Math.random() * 10);
    		System.out.println("I'm thinking of a number between 0 and 9...");
    		
    		while(true) {
    			System.out.println("number to guess is " + targetNumber);
    			
    			p1.guess();
    			p2.guess();
    			p3.guess();
    			
    			guessp1 = p1.number;
    			System.out.println("Player one guessed " + guessp1);
    			
    			guessp2 = p2.number;
    			System.out.println("Player two guessed " + guessp2);
    			
    			guessp3 = p3.number;
    			System.out.println("Player three guessed " + guessp3);
    			
    			if (guessp1 == targetNumber) {
    				p1isRight = true;
    			}
    			if (guessp2 == targetNumber) {
    				p2isRight = true;
    			}
    			if (guessp3 == targetNumber) {
    				p3isRight = true;
    			}
    			
    			if (p1isRight || p2isRight || p3isRight) {
    			
    				System.out.println("We have a winner!");
    				System.out.println("Player one got it right? " + p1isRight);
    				System.out.println("Player two got it right? " + p2isRight);
    				System.out.println("Player three got it right? " + p3isRight);
    				System.out.println("Game is over.");
    				break;
    		    } //end if
    				else {
    				
    				System.out.println("Players will have to try again. ");
    				}//end if/else
    			}//end loop
    		}//end method
    	}//end class
    ///////////////////////////////////////////////////////////////////////////////////////////////////////	
    
    public class Player {
    	int number = 0; //where the guess goes
    	
    	public void guess() {
    		number = (int) (Math.random() * 10);
    		System.out.println("I'm guessing " + number);
    	}//end guess
    }//end player class
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////
    
    public class GameLauncher {
    	public static void main (String[] args) {
    		GuessGame game = new GuessGame();
    		game.startGame();
    	}
    }
    Last edited by silverglade; 04-26-2011 at 12:31 AM.

  4. #4
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    The best solution is to really have 3 separate classes.
    And did you know that the source files need to be called the same way
    as the class, else it won't compile :)?

    So you'll need 3 files, GuessGame.java, Player.java and GameLauncher.java which you'll have to compile separately.

    However, thats not what you asked, but since what you asked is such a bad design - you can read on how to do it by yourself :D.

    Search google for "java inner classes" or just look here:

  5. #5
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    thank you for that. I now have 3 separate java .class files in my project folder, I want to play my game, but none of the .class files reference each other. Do I have to put some kind of include statement at the top for the less important 2 classes? please. I don't know what the syntax of that would be. thank you for helping. Derek

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    if I have to compile more than one file, I don't know how to do that.

    If the classes are part of the default package (yours are at the moment), the commands to compile and then run the game are:

    Java Code:
    javac -cp . *.java
    java -cp . GameLauncher

    The commands are most easily invoked from the directory (folder) where your source (.java) files live. The "-cp ." bit is telling the java and javac executables that the classes are to be located in the current directory.

    ----------------------

    As things get more complex - you want to use packages, and/or you want to compile from other locations or with other options - you should read the man pages for java and javac.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Do I have to put some kind of include statement at the top for the less important 2 classes?

    That's a good question. Unlike other languages you are not really linking at the time you compile your classes into byte code, so you don't need to include anything.

    The -cp part of the cammand is all that either java or javac need to locate the classes.

  8. #8
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Man this is humbling. The beginning of programming is very humbling. Anyway thank you for that taking the time. It worked GREAT. And let me say to other newbies, "does running your first semi large newbie program feel like giving birth? Because if men could give birth, I just did" LMAO. thank you.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Lots of spammers. You can have a file with multiple classes as well. You need to have one class which is public and has the same name as the rest of the file, the rest of the classes can have default(package) access.

    Java Code:
    public class X{}
    class Y{}
    class Z{}
    Can all be in one file and you won't have any errors, however;
    Java Code:
    public class X{}
    public class Y{}
    public class Z{}
    will cause an error.

  10. #10
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    OP,

    You might have come to know by now that any Java file can have only single public class. But sometimes the things get trickier when you come across the inner classes. So I would suggest you to go through some good documentation about the inner classes and their syntax. Google your way through it.

    Here is a good link to start with : Java Inner Classes

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

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

    Default

    Javac is a bit more than a compiler in the traditional sense; if it know where to find the .class files (the -cp flag) and it knows where to find the .java source files (the -sourcepath flag) and it has to compile one of these sources it checks for .class files your source depends on. If they are newer than the source files for those classes it checks the .class files, otherwise it compiles the other source files behind your back so it has up to date .class files. Only then it compiles the source file you wanted to compile. If you carefully arrange your dependencies and compile the 'main' .java file all files will be compiled in your project.

    The scenario makes some 'make' utilities go dizzy ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Class is public, should be declared in a file
    By goldhouse in forum New To Java
    Replies: 7
    Last Post: 05-10-2012, 09:17 AM
  2. Replies: 1
    Last Post: 10-08-2010, 02:19 AM
  3. Public, private or (nothing) class
    By tyang in forum New To Java
    Replies: 3
    Last Post: 01-31-2010, 11:37 PM
  4. Use an internal Method or Make a class file?
    By TimHuey in forum New To Java
    Replies: 2
    Last Post: 09-18-2009, 03:06 PM
  5. different multiple public class and main class
    By mr idiot in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 12:10 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
  •