简体   繁体   中英

Java inner class vs external class

So I have to design a DTO in Java, in a way that I have a Profile which has many Roles. This DTO will be served to a front end application. So I create a class named ProfileDTO and a static inner class called Role.

public class Profile {

        private List<Role> roles;

        // ommiting getters/setters

        public static class Role {
        }

This way, if I want to create an instance of Role from outside I have to declare it like

var role = new Profile.Role();

I also saw another approach from a colleague of mine though. Have a separate Role class in the same package and just use

private List<Role> roles;

in Profiles class.

So which approach is better? And if it depends, what are the factors it depends on?

Actually it depends on how you want to represent your domain. In example, if you want the Role class to be accessible to every other class or to have a specific visibility you could create an external class; otherwise, you could use an inner class to have explicitly stated the relation between the Role class and the Profile one.

To cite thetutorial from oracle docs :

Compelling reasons for using nested classes include the following:

  • It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.
  • It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
  • It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.

Moreover, regarding the static nested class,

A static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.

If the inner class is only used by the outer class, I will prefer your approach. It has an object-oriented advantage, an organizational advantage, and a call-back advantage according to https://www.infoworld.com/article/2077411/inner-classes.html . Simply speaking, it means the inner class is logically binded to the outer class so that the inner class can be easily accessed and maintained within the outer class.

However, if the above advantages disappear, inner class will remain code complexity and probably create redundant class for the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM