Initialize final variable within constructor in another method

前端 未结 3 1219
心在旅途
心在旅途 2021-01-12 04:10

I have a problem which isn\'t really that big, but still gives me some thought as to how Java constructors and methods are used.

I have a constant representing a rad

3条回答
  •  北海茫月
    2021-01-12 04:57

    How about (using small caps for radius, because it is not a constant, as pointed out in the comments):

    public MyProblematicClass(... variables ...) {
        radius = getRadiusWithCriteria(criteria);
    }
    
    private int getRadiusWithCriteria(criteria crit) {
       if(... crit ...) {
          return n;
       } else {
          return 0;
       }
    }
    

提交回复
热议问题