Reputation: 211
why cant i use static as a variable modifier in a constructor and would final work for eg construcor eg in my code below i want to initialise the variable times to a constant of 15 so that whenver the constructor is created in the main program
public class RegularProcedure {
// the duration period of a regular procedure is 15
int []procedure;
public RegularProcedure(int t){
final int times=15;
procedure=new int[times];
for(int i=0; i <procedure.length;i++){
procedure[i]=i;
}
}
}
Upvotes: 0
Views: 111
Reputation: 308031
times
is a local variable and static
doesn't make sense for local variables.
You can put static final int TIMES = 15
just above (or below) the definition of procedure
and it will work just fine. That's a common idiom for defining constants in Java.
Upvotes: 1
Reputation: 240900
What ever thing you declare in const. will be inside block (local) only.
static
is meant to be at class level associated with class
Upvotes: 0
Reputation: 12633
Because the Constructor is to do with Objects i.e. it creates an Object from the Class blueprint. Static variables belong to the Class itself so they must be at Class level.
And no final wouldn't work. That just means the reference can't change after it's been assigned.
Upvotes: 0