Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple classes for one program  RSS feed

 
Mrinal Roy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My problem is .java file has references to multiple classes outside the main. How to save these classes - in same main method file or in separate files? And how to name the files for these class references?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should save each top-level(*) class in a separate file, whose name is the name of the class, plus ".java".

For public classes, the above is actually a firm requirement of Java. If you try to put more than one public class in a file, or to name them differently, you get a compiler error.

For non-public classes, the above is not required. But it is good practice to do it anyway. That way, it is always easy to find which file contains the source code for a particular class.

(*) top-level means any class that is not declared inside another class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!