File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initializing final memeber variable

 
Philosopher
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mudassar Shafique
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1070
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2166
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic