How to create a variable that can be set only once but isn't final in Java

后端 未结 12 1592
感情败类
感情败类 2020-12-14 05:43

I want a class that I can create instances of with one variable unset (the id), then initialise this variable later, and have it immutable after initial

12条回答
  •  孤城傲影
    2020-12-14 06:18

    try have an int checker like

    private long id = 0;
    static int checker = 0;
    
    public void methodThatWillSetValueOfId(stuff){
        checker = checker + 1
    
        if (checker==1){
            id = 123456;
        } 
    }
    

提交回复
热议问题