Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static final Array

 
Gautham Muralidharan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above code doesn't compile. I get an error that final field cannot be assigned.

But the following code compiles finely.



How does using a static block in line 4 prevents me from getting error? The array a is still final, right? Why i'm not getting the same error that final field cannot be assigned?

Thanks in advance.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gautham Muralidharan wrote:The above code doesn't compile. I get an error that final field cannot be assigned. 

static fields of a class are initialized when class is initialized on It's first loading. After that instance fields of a class are initialized only If you create an object of that class.

final static fields of a class are initialized either by field initializer Or static initializer. These field initializers and static initializers are executed when the class is initialized on It's first loading. Means static fileds get initialized before you create an object of a class.

Initialization block are executed each time you create an object, to initialize instance fields. As we know static fields gets initialized before this so in your first example an array of type int a is a final static field. Being static final field It must to be initialized only once when the class is loaded and initialized but you are trying to initialize It in an initialization block which is executed after static filed initialization order so It gives error.

How does using a static block in line 4 prevents me from getting error? The array a is still final, right? Why i'm not getting the same error that final field cannot be assigned? 

Yes array is still final. As I mentioned above final static fields can be initialized using field initializer i.e. initialize final static field during declaration Itself like
Or in static initializer block as you did in your second example which are executed when class is initialized. So this final static field is initialized only once. That is why in second example It doesn't give error.

You can read this --->Order of initialization to know more.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gautham Muralidharan wrote:How does using a static block in line 4 prevents me from getting error? The array a is still final, right? Why i'm not getting the same error that final field cannot be assigned?

Because you have initialized the final array in a static initializer block. In the first code snippet you have used an instance initializer block. Are you familiar with the differences between a static and an instance initializer block?

In this topic you'll find a nice overview of the initialization of class, instance and local variables (even when they are marked final). Definitely worth reading!

Hope it helps!
Kind regards,
Roel
 
Gautham Muralidharan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Ganesh and Roel.
Once again you both have helped me.
Now I have learnt more about initialization thanks to your help.
Both the links were really informative.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome, If you search in this forum by any topic you have problem about. You will get so many such informative links for sure.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic