Forums Register Login

Initializing final memeber variable

+Pie Number of slices to send: Send
In the java ranch game, It says that earlier versions of jdk restricted the programmer to initialize the final member variable within constructor or somewhere but current versions dont. I tried this thing with jdk1.2.2 and it forces me to initialize the final memeber variable. So the answer is TRUE not false. what do you say?
+Pie Number of slices to send: Send
Hi,
In JDK1.2.2 its possible, to initialize a final var in the constructor.
The code below #1 & #2 explains it
class FinalTest
{
final int i;
FinalTest()
{
//i = 9;
}
void setI(int i)
{
this.i = i;
}
public static void main(String args[])
{
FinalTest ft = new FinalTest();
ft.setI(99);
//System.out.println(new FinalTest().i);
System.out.println(ft.i);
}
}
> javac FinalTest.java
final int i;
^
E:\jdk1.2\bin\FinalTest.java:10: Can't assign a second value to a blank final variable: i
this.i = i;
^
2 errors
Tool completed with exit code 1
---------------------------------------------------------------
class FinalTest
{
final int i;
FinalTest()
{
i = 9;
}
public static void main(String args[])
{
FinalTest ft = new FinalTest();
System.out.println(ft.i);
}
}
>javac FinalTest.java
>java FinalTest
9
thnks & rgds
+Pie Number of slices to send: Send
Thats what I am saying that jdk1.2.2 forces you to initialize the final variable in the constructor or through some initializer. Whereas the game says: earlier versions forced to initialze a final variable but current versions don. I think the answer should be changed.
+Pie Number of slices to send: Send
Hi,
I think you are all talking in circles! The Java Ranch answer is correct. As stated by all other responses. Let's rewrite the question:
Original:
Earlier versions of JDK restricted the programmer to initialize final member variables within constructor or somewhere, but current versions don't. TRUE or FALSE?
Rewrite:
JDK 1.2 doe not required the programmer to initialize final member variables within constructor or somewhere. TRUE or FALSE?
As all replies (including the original question!) has rightly stated:
Original:
Earlier versions of JDK restricted the programmer to initialize final member variables within constructor or somewhere and so does the current version!!
Rewrite:
JDK 1.2 DOES require the programmer to initialize final member variables within constructor or somewhere!!
Therefore the answer to the question is FALSE (Yeah Java Ranch!).
Regards,
Manfred.
+Pie Number of slices to send: Send
Philosopher,
Your name doesn't comply with our naming policy here at the Ranch. Take a look here for more info: www.javaranch.com/name.jsp
Please re-register with a more appropriate name.
Thanks,
Bill
+Pie Number of slices to send: Send
Hi Manfred,
but it doesn`t seem to me a question about programming, but about Hm logic. O.k. logic has to do something with programming.
But I think the question leads people who are still learning Java (and not advanced logic) on a wrong trail.
Axel
+Pie Number of slices to send: Send
Hi all,
I believe that the current release is 1.3, so doesn't that make the 1.2.2 version a 'previous' release?
I still don't know if the v1.3 compiler works in this case..
-Abhijit
Did you miss me? Did you miss this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1427 times.
Similar Threads
about contructor
abou final variable
final static variable
final with static
final variable in switch case
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:58:54.