简体   繁体   中英

Initializing pojo class with sub classes in one time

Is it possible to initialize class with it's sub-classes in one time instead of initialize each sub class by its own? Example:

public class Abc {

    public Abc1 abc1;
    public Abc2 abc2 ;
    public Abc3 abc3;

    public static class Abc1 {
        public String id;
    }

    public static class Abc2 {
        public String organization;
    }

    public static class Abc3 {
        public String location;
    }
}

In order to be able to fill id, organization or location i need to manually initialize each sub-class like that:

Abc abc = new Abc();
abc.abc1= new Abc.Abc1();
abc.abc1.id = "123";

abc.abc2= new Abc.Abc2();
abc.abc2.organization = "Google"

abc.abc3= new Abc.Abc3();
abc.abc3.location = "New York";

If I will not initialize the sub-classes, I'll get null pointer exception when I will try to fill their attributes Is there any trick to skip all that initializations in order to fill all the pojo attributes?

Abc1 , Abc2 , Abc3 are not sub-classes , since they do not extends Abc class. Instead they are inner classes. In order to initialize them at once you can create a constructor in Abc which will also initialize those classes.

For example, constructor in Abc class:

    public Abc() {
        this.abc1 = new Abc1();
        this.abc2 = new Abc2();
        this.abc3 = new Abc3();
    }

Then you just need to initialize Abc class via this constructor:

public static void main(String[] args) {
    Abc abc = new Abc();

    abc.abc1.id = "123";
    abc.abc2.organization = "Google";
    abc.abc3.location = "New York";

}

You must create the class objects via new somewhere, it won't be done automatically. But you can do it within the class Abc itself, rather than outside of the class.

For example, you can add field initializations to their declarations (see Java tutorial ):

public class Abc {

    public Abc1 abc1 = new Abc1();
    public Abc2 abc2 = new Abc2();
    public Abc3 abc3 = new Abc3();

    // ...

}

Abc abc = new Abc();
// abc.abc1, abc.abc2, abc.abc3 are already initialized

abc.abc1.id = "123";
abc.abc2.organization = "Google"
abc.abc3.location = "New York";

Or you may do this in constructor:

public class Abc {

    public Abc() {
        this.abc1 = new Abc1();
        this.abc2 = new Abc2();
        this.abc3 = new Abc3();
    }

    // ...

}

In addition to the previous answers (which you should keep in mind when designing your classes / modeling your data), you could create a constructor that directly sets the values for the inner classes:

public class Abc {

    public Abc(String id, String organization, String location) {
        this.abc1 = new Abc1();
        this.abc1.id = id;
        
        this.abc2 = new Abc2();
        this.abc2.organization = organization;
        
        this.abc3 = new Abc3();
        this.abc3.location = location;
    }

    public Abc1 abc1;
    public Abc2 abc2 ;
    public Abc3 abc3;

    public static class Abc1 {
        public String id;
    }

    public static class Abc2 {
        public String organization;
    }

    public static class Abc3 {
        public String location;
    }
}

And then just call:

Abc abc = new Abc("123", "Google", "New York");

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