The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


I tried, but it still failed!

Posted by Yuan YUAN on November 01, 2001 at 12:32 AM

Thanks for you answer. I tried just the same as you've done,
but it still didn't work.
The javac compiler said, 'cannot assign value to final variable myVariable' when I initialised it.
Can you try it again? Thanks a lot!

> Yuan,

> The important thing to remember here is that although a variable declared final cant be changed, the key here is that it cant be changed "once it has been initialised". Take the following example:-


> public class MyClass {
> public final String myVariable;
> public String myMethod() {
> // here, myVariable can be initialised
> .
> .
> myVariable = "a value";
> // here, myVariable has been set and cannot be changed.
> }
> }


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us