开发者

variable modifiers in constructors

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 RegularProc开发者_如何学Cedure(int t){
         final int  times=15;
         procedure=new int[times];
         for(int i=0; i <procedure.length;i++){
             procedure[i]=i;
         }
     }
}


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.


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.


What ever thing you declare in const. will be inside block (local) only.

static is meant to be at class level associated with class

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜