There is a task: to create a class and its successor. Should classes be created in a single java file or should each be in its own file with a .java ?

  • Depends on the task. - Alexey Shimansky
  • For example, a man, a student of the first inheriting - Muscled Boy
  • one
    I meant from the vastness of the task. If, in consequence, heirs are planned, if classes are not small, it is planned to expand with functionality, etc. - then of course different classes ..... And if, roughly speaking, you only need to do the task of inheritance, in order to understand the lesson, with a small set of fields, then there is no special meaning. So in your case, you can probably get by with just one file .... But in general, classes are divided by files - Alexey Shimansky
  • one
    It should be borne in mind that the public name of the top-level class must match the name of the file, so there will be only one such class in the file. This is the only real limitation. Small tasks, so as not to produce projects and not litter the namespace in one project, you can create a separate sub-package for each lesson, or use nested static classes in a class with main . - zRrr
  • thanks, I'll keep in mind - Muscled Boy

1 answer 1

In your case, it is better to create both classes in one file, but it is always better to create each class in a separate file.

  • and create a project with the main class of "man"? and explain, please, why they are usually created in different files - Muscled Boy
  • one
    As a rule, the main class Main with an entry point public static void main(String[] args) - Alexey Shimansky
  • that is, the classes “person”, “student”, “main” (there will be no fields and methods other than the main one, and there are “basic actions” in it — creating instances of the classes “person” and “student”, for example)? do I understand you correctly? - Muscled Boy
  • one
    main() is the main method. This is the line from which the program starts. Point of entry. All Java applications should have one main() method. ...... A class that does not have a main method can be successfully compiled, but cannot be executed, because it does not have a starting point of execution, which is the main() method - Alexey Shimansky
  • one
    @MuscledBoy The class with the entry point main() can be named as you like, even though the class RahamBaharamamBuru . The main thing that was the main class with the entry point. Simply, this main class is often called the Main (with a capital letter) ..... catches the eye and immediately intuitively understands what kind of class it is - Alexey Shimansky