简体   繁体   中英

how to set the values for a public static final class instance in java

example

public static final class MyClass{
        private long id_;
        public static final int MEMBER_ID_FIELD_NUMBER = 2;
        private long startDate_;
    ....

    }

//meanwhile in another class
class Abc {
        ...
    public final void aFunc {
        MyClass aaa = ???? // here is what I want to know how to set the class values
    ....
    }

}

How can I set the value for MyClass in the other class? I tried to do "initialize" like

MyClass aaa = {
                id_: 123123132,...
            };

it just give me error that id_ `can't resolve symbol.

I come from javascript background, that's why I think like this. Any idea how can I initiate the instance with partial / all custom default values for a public static final class ? Btw, if the back-story can help get a full picture --- I do this for testing a specific class's method which is including an instance of MyClass . This is an attempt to mock the values instead having to recreate a whole ActionBean to just test one method.

If it has default constructor (the one without any argument) you can first create object without providing fields values and the set it with setters (if they exist) like so:

public final void aFunc {
        MyClass aaa = new MyClass();
        a.setId_(1);
        ...
}

If it doesn't have default constructor you need to provide all parameters during object creation, like so:

public final void aFunc {
        MyClass aaa = new MyClass(1,2,3);
        ....
}

There are multiple ways to create an object instance and assign values to fields:

  1. Call setter methods:

     public static final class MyClass { private long id_; public void setId(long id) { this.id_ = id; } }
     MyClass aaa = new MyClass(); aaa.setId(123123132);
  2. Use a constructor with arguments:

     public static final class MyClass { private long id_; public MyClass(long id) { this.id_ = id; } }
     MyClass aaa = new MyClass(123123132);
  3. Use public fields. Although not recommended, this is what JavaScript kind of does:

     public static final class MyClass { public long id_; }
     MyClass aaa = new MyClass(); aaa.id_ = 123123132;
  4. Use a builder:

     public static final class MyClass { private long id_; private MyClass(long id) { this.id_ = id; } public static class Builder { private long id_; public void id(long id) { this.id_ = id; } public MyClass build() { MyClass x = new MyClass(); x.id_ = this.id_; return x; } } }
     MyClass aaa = new MyClass.Builder() .id(123123132) .build();

You have two options in order to fill the fields with values. One options is using the public constructor passing all the parameters you want to update:

public static final class MyClass{
    private long id_;
    public static final int MEMBER_ID_FIELD_NUMBER = 2;
    private long startDate_;

    public MyClass(long id_, long startDate_) {
        this.id_ = id_;
        this.startDate_ = startDate_;
    }
}

You can use it as bellow:

public final void aFunc {
    MyClass objectA = new MyClass(1L, 23214112213L);
}

Second option is using setters: method per field to add value:

public static final class MyClass{
    private long id_;
    public static final int MEMBER_ID_FIELD_NUMBER = 2;
    private long startDate_;
    
    public void setID(long id_) {
         this.id_ = id_;
    }

    public void setStartDate(long startDate_) {
         this.startDate_ = startDate_;
    }
        
}

You can use them as bellow:

public final void aFunc {
    MyClass objectB = new MyClass();
    objectB.setID(1L);
    objectB.setStartDate(23214112213L);
}

But you can combine them. You can have the constructor and bellow the setters.

See java constructor and see setters and getters

  • For you private fields i assume you need getters too.

Those are part of the basic knowledge you should have. In addition keep in mind there are conventions (naming etc)

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