Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multiple classes

 
Kevin Carr
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I am just beginning to learn java, started a day ago, and I ran into some trouble when compiling a program. It has multiple classes, one of them being the main, so I saved the file as the same name as the main one. when I tried to compile it, the line said to save each class separate. what am i supposed to do?
 
Shevesh Srivastava
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you give your code here.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each public top-level class must be defined in a file with the same name and ending with ".java". For instance, public class "String" must be located in file "String.java", etc.

In your case you have two options:
1) remove the public modifier for each class. They will have default access, and you are allowed to have multiple classes with default access in one file.

2) split the Java file such that you have one file for each (public) class. This is my preferred solution, as you also keep your source files smaller.
 
Kevin Carr
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im going through the book head first java second edition. Im stuck on the guessing game app. There are three classes, one main, and I am unsure of whether to break it into multiple files or not, and how to do that if am to do that.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have three classes in your file, right? Something like this (I'm using random names since I don't have the book):
You would then create three files as follows:
 
Kevin Carr
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K, thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic