1

I'm just starting with Java, although I already have some experience with OOP in PHP

In this example, I have a simple class called Person, with fields such as name, age, and sex. I was wondering what is considered the best practice, to do something like this:

public class Main {
    public static void main(String[] args){
        Person customer = new Person("Jonh Smith", 21, 'M');
        System.out.println(customer.name);
    }

    public static class Person {
        public String name;
        public int age;
        public char sex;

        public Person(String name, int age, char sex){
            this.name = name;
            this.age = age;
            this.sex = java.lang.Character.toUpperCase(sex);
        }
    }
}

In this way, the Person class needs to be static for it to work, and this is what made me wonder that it might not be the best way to do it, here is the other way:

public class Main {
    public static void main(String[] args) {
        Person customer = new Person("Jonh Smith", 21, 'M');
        System.out.println(customer.name);
    }
}
    
class Person {
    public String name;
    public int age;
    public char sex;

    public Person(String name, int age, char sex){
        this.name = name;
        this.age = age;
        this.sex = java.lang.Character.toUpperCase(sex);
    }
}

So, what's the best way to do this moving forward? I will still need to add other classes, like Company.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Levy Barbosa
  • 342
  • 1
  • 5
  • 13

3 Answers3

4

The preferred way is to create top-level classes in separate files. That is, you have a Main.java with the class Main, and a Person.java with the class Person.

Creating multiple top-level classes in a single file is allowed in Java (although only one class can be public, and if public, that class' name must match the filename), but its use is generally discouraged. If you do want to have multiple classes in a single file, the basic advice is "don't do it", and if you really want to, then the recommendation is to use nested classes (like your first example), but generally only when those classes are tightly coupled (and for example need access to each others internals).

Related:

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • So, I should always separates the classes in different files? Even if they're really simple ones, like the Person in the example? In this case, I think that I should keep everyhing in a single file, since this is a relatively simple college assignment and I believe that this is the way my professor intend to receive it, as a single file – Levy Barbosa Apr 28 '21 at 16:19
  • @LevyBarbosa In general, yes. For purposes of source code organization it is considered cleaner, and less surprising and aids discoverability of your code. That said, if this is a simple assignment, and you don't expect this to be reused or grow into something bigger, than you can get away with either solution of your question. This might even be a case where defining both `Main` and `Person` as a top-level class might be preferable (instead of nesting `Person` in `Main`). – Mark Rotteveel Apr 28 '21 at 16:20
2

It is more of a choice of preference, but looking at https://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html

and https://docs.oracle.com/javase/tutorial/java/javaOO/whentouse.html

It encourages the use of inner classes when only that class itself will be using it. In the case of Main, I personally recommend splitting it into separate files instead of putting Customer + the rest of your classes in the same class.

LeoDog896
  • 3,472
  • 1
  • 15
  • 40
2

There isn’t really any advantage to nesting the classes except that things fit in one file. But the idea of putting classes in the same file doesn’t scale at all, even adding one more class you can see your one file will get a bit awkward to deal with.

There is an idea of separating things so there are not multiple reasons to change one file. Putting your classes in separate files helps you toward that aim. Also separate classes are what you will see in real world projects.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • So, "in the real world", i should have a separate file called Person.java? With only that class on it? Even if it's a really simple one? In this particular case, i have to make this relatively simple program for a college assignment, so it's probably better that i fit everything in a single file – Levy Barbosa Apr 28 '21 at 16:06
  • 1
    @Levy: yes in projects you do at work it’s going to be mostly one class per file. Even a smallish project may end up having hundreds of classes. For school projects definitely fitting things in one file may be more convenient. – Nathan Hughes Apr 28 '21 at 16:11