wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final variable initialisation(simple code) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final variable initialisation(simple code)" Watch "final variable initialisation(simple code)" New topic
Author

final variable initialisation(simple code)

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


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?

SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Jun 23, 2010
Posts: 432

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

Joined: Nov 04, 2009
Posts: 2066

So, you've cleared it!
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

YUP.
thanks for quick reply
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

You are Welcome!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final variable initialisation(simple code)