3

If I set a value of an attribute of a class and use final, do i have to initialise the attribute as a parameter of the class constructor? For example, I have set the attribute rack of length to a value of 60

public class Runs {
    Teams team;
    final int racklength=60;
    int xposteam;

     public Runs (Teams y) {
         team=y;           
     }
}
1
  • You can format your code by indenting the code block by 4 spaces (or select the code section and click the {} button in the editor). I did that for you, but you might want to edit the question and try to fix the formatting a little more.
    – Andy White
    Apr 27, 2011 at 0:30

4 Answers 4

3

final fields have to be either initialized inline or in constructor.

Example:

class Foo {
    final String bar = "bar";
    Foo() {
    }
}

or

class Foo {
    final String bar;
    Foo() {
        bar = "bar";
    }
}

is acceptable.

1

You must always initialize a final attribute, but you can do it either in-place (where you first declare it) or in its constructor (or constructors):

public class Runs {
    Teams team;
    final int racklength; // initialization postponed
    int xposteam;

     // constructor defaulting racklength to 60
     public Runs (Teams y){
         team=y;
         racklength=60;
    }

     // constructor with variable initialization of final attribute
     public Runs (int l, Teams y){
         team=y;
         racklength=l;
    }

     // error since racklength is not initialized during construction
     public Runs (){
         team=null
    }
}
1

No, you can initialize it where you declared the variable. And theres no reason you have to assign the variable with a parameter to the constructor.

2
  • "And theres no reason you have to assign the variable with a parameter to the constructor." - Maybe I'm reading you wrong, but... uh? If you define an instance-level constant (which a non-static final really is), you can initialize it via a constructor parameter. Otherwise, if all instances of a class are to have the same value for their instance-level constants, we might as well make the constant static. Apr 27, 2011 at 0:38
  • 1
    I agree. Your point is not mutually exclusive with what I said. Apr 27, 2011 at 0:47
1

No you do not have to do so.

Both of these work fine.

public final class SomeClazz {
    private final int test=10;
    public SomeClazz(){

    }
    public int getTest() {
        return test;
    }

}

Or

public final class SomeClazz {
    private final int test;
    public SomeClazz(int test){
        this.test = test;
    }
    public int getTest() {
        return test;
    }

}

However, I prefer to initialize them via the constructor because that way the caller who is constructing the object knows how to create the state of the object with the intended values. If it's supposed to be a constant type value then you should make use of Enums rather than defining them in your class this way.

4
  • You don't need a getter in order to do this.
    – Jeremy
    Apr 27, 2011 at 1:25
  • @Jeremy Heiler - right. I meant to declare test as private not the default package private. I have updated my post.
    – CoolBeans
    Apr 27, 2011 at 2:45
  • You completely missed my point. There is absolutely no need to declare a getter in order to make an class/instance variable final.
    – Jeremy
    Apr 27, 2011 at 3:25
  • Aah - I just re-read my first line in the post. I got it. Thanks!
    – CoolBeans
    Apr 27, 2011 at 3:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.