Results 1 to 11 of 11
- 04-25-2011, 11:43 PM #1
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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! Derek
confused:
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(); } }
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.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
Last edited by silverglade; 04-26-2011 at 12:07 AM.
-
- 04-26-2011, 12:11 AM #3
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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.
- 04-26-2011, 12:46 AM #4
Member
- Join Date
- Apr 2011
- Location
- Canada!
- Posts
- 30
- Rep Power
- 0
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:
- 04-26-2011, 12:58 AM #5
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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
- 04-26-2011, 12:59 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 04-26-2011, 01:02 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 04-26-2011, 01:08 AM #8
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 5
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.
- 04-26-2011, 05:38 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
Can all be in one file and you won't have any errors, however;Java Code:public class X{} class Y{} class Z{}
will cause an error.Java Code:public class X{} public class Y{} public class Z{}
- 04-26-2011, 05:45 AM #10
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,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 04-26-2011, 07:47 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Class is public, should be declared in a file
By goldhouse in forum New To JavaReplies: 7Last Post: 05-10-2012, 09:17 AM -
JCreator problem; class Exercise1 is public, should be declared in a file named Exerc
By JehanMustafa in forum New To JavaReplies: 1Last Post: 10-08-2010, 02:19 AM -
Public, private or (nothing) class
By tyang in forum New To JavaReplies: 3Last Post: 01-31-2010, 11:37 PM -
Use an internal Method or Make a class file?
By TimHuey in forum New To JavaReplies: 2Last Post: 09-18-2009, 03:06 PM -
different multiple public class and main class
By mr idiot in forum New To JavaReplies: 2Last Post: 01-01-2009, 12:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks