aspose file tools*
The moose likes Mock Exam Errata and the fly likes Initializing final memeber variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Initializing final memeber variable" Watch "Initializing final memeber variable" New topic
Author

Initializing final memeber variable

Philosopher
Greenhorn

Joined: Mar 15, 2001
Posts: 6
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
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
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
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
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
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
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
 
wood burning stoves
 
subject: Initializing final memeber variable