Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final variable initialisation(simple code)

 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

howdy.
above code works fine.
1. if we make above final variable static it is giving compiler error saying "cannot assign value to a final variable"??

In my opinion,if an instance variable has been marked final,it has to be initialized at the same line.
or it has to be initialized at the end of every constructor of the class.(till now am right i guess)

2. It is a compile-time error if a blank final class variable is not definitely assigned by a static initializer of the class in which it is declared.(please explain this statement)

3. but here we have replaced constructor with static initialize block,this works fine,please give reason for this also.
"is this mean that if a final instance variable has been made static also it has to be initialized in the static initializer block or on the same line."
does above line make sense?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arjun Srivastava wrote:
In my opinion,if an instance variable has been marked final,it has to be initialized at the same line.
or it has to be initialized at the end of every constructor of the class.(till now am right i guess)

That's correct!
Arjun Srivastava wrote:
2. It is a compile-time error if a blank final class variable is not definitely assigned by a static initializer of the class in which it is declared.(please explain this statement)

"is this mean that if a final instance variable has been made static also it has to be initialized in the static initializer block or on the same line."
does above line make sense?


static variables are loaded at the class loading time. Then how can you initialize it within a method creation?
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:
static variables loaded at the class loading time. Then how can you initialize it within a method creation?

which method you are talking about?
code at line 3 works fine.
and line 2 "It is a compile-time error if a blank final class variable is not definitely assigned by a static initializer of the class in which it is declared." what does it mean?
at code at line 1 if we make above final variable static it is giving compiler error saying "cannot assign value to a final variable"?? why so?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arjun Srivastava wrote:
which method you are talking about?

I don't mean any methods. static variables are belonging to Class not to objects. Then how can you initialize it within object creation chain?
Arjun Srivastava wrote:
and line 2 "It is a compile-time error if a blank final class variable is not definitely assigned by a static initializer of the class in which it is declared." what does it mean?

It should be static final blank variables.
Arjun Srivastava wrote:
at code at line 1 if we make above final variable static it is giving compiler error saying "cannot assign value to a final variable"?? why so?

Because, you are initializing static variable in a object creation.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:
Because, you are initializing static variable in a object creation.

ok now it can be digested.thanks
that simply means static instance variable can't be initialize inside the constructor.

Abimaran Kugathasan wrote:
and line 2 "It is a compile-time error if a blank final class variable is not definitely assigned by a static initializer of the class in which it is declared." what does it mean?
It should be static final blank variables.

yes ,here it should be the static final blank variables
by the way line 2 i have copied from java sun tutorial.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you've cleared it!
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YUP.
thanks for quick reply
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are Welcome!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic