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?
Naveen Arumugam
Greenhorn
Joined: Mar 10, 2001
Posts: 23
posted
0
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
ANaveenS
Mudassar Shafique
Greenhorn
Joined: Mar 16, 2001
Posts: 10
posted
0
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.
Manfred Leonhardt
Ranch Hand
Joined: Jan 09, 2001
Posts: 1492
posted
0
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.
bill bozeman
Ranch Hand
Joined: Jun 30, 2000
Posts: 1070
posted
0
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
Axel Janssen
Ranch Hand
Joined: Jan 08, 2001
Posts: 2164
posted
0
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
abhijit r
Greenhorn
Joined: Mar 07, 2001
Posts: 4
posted
0
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
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.