Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static final

 
Max White
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When I use the following code ..

or the following


The code gets compiled.

But not when I use the following


Error : Final variable may not have been initialised.

My doubt is that it works if I just use either a final or a static variable and the variable gets the default value of null.
Why does it not work with static final ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final instance/class variable of class should assign a value in constructor(other word on declaration) else error.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you know unfortunately final modifier is unnoticed between developer except in context of constant....
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from the JLS

8.3.1.2 final Fields
A field can be declared final (§4.12.4). Both class and instance variables
(static and non-static fields) may be declared final.
It is a compile-time error if a blank final (§4.12.4) class variable is not definitely
assigned (§16.8) by a static initializer (§8.7) of the class in which it is
declared.
A blank final instance variable must be definitely assigned (§16.9) at the end
of every constructor (§8.8) of the class in which it is declared; otherwise a compile-
time error occurs.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet reply is good ...
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:gurpeet reply is good ...


or you should say JLS's reply is good
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Case 1 :
Max White wrote:



How do we get to use this final variable? We would have to create a Test object through "new Test()" and this will further call the parameter-less constructor which would set the value of a to default null. You would have no problem.

Case 2:
Max White wrote:


In this case can we access the final variable without a constructor? The answer is yes. Final variables not assigned during declaration are called blank final variables and its a compile time error to use them without assignment. The only difference with Case 1 is the fact that the assignment is taken care in the constructor call.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Praveen , i have to correct you in case 1 .

contrary to what you said, Case 1 also won't compile. i'm quoting again what i wrote above from the JLS

A blank final instance variable must be definitely assigned (§16.9) at the end
of every constructor (§8.8) of the class in which it is declared; otherwise a compile-
time error occurs.


here definitely assigned means that we should assign it either while declaring it or in the constructor or in the instance initializer.

the code snippet in case 1 wont compile so there won't be any default value assignment to String instance member

the same thing holds for case 2 as well.

regards
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I stand corrected! You need to assign a value before the first use of the variable(and not the default null).
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praveen Kumar M K wrote:Yes, I stand corrected! You need to assign a value before the first use of the variable(and not the default null).


i do not know what you are trying to say. i may be wrong in interpreting you . all i want to say is that case 1 won't compile. you have to explicitly initialize final instance or class variables in their respective initializers.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I meant that I was wrong(concerning Case 1), thanks for the correction!
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are welcome buddy
 
Max White
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the answers.
Gurpeet is right,case 1 wont compile.Its my mistake.
 
Mrinal Singhania
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic