Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / August 2005

Tip: Looking for answers? Try searching our database.

public static final - compilation error

Thread view: 
Kamil Roman - 28 Jun 2005 16:49 GMT
Hi,

is it possible to initialize a public static final member of a class in
the class' constructor? I try to do this and I receive error message:

variable PLUS is declared final; cannot be assigned.

The code is:

public class Stale
{
 public static final String[] OPERATORS = new String[]
 {
   new String("+"),
   new String("-"),
   new String("*"),
   new String("/"),
 };
 public static int PLUS;  //final
 public static int MULTI; //final
 public static int MINUS; //final
 public static int DIVIDE;//final
 public Stale()
 {
   for (int i = 0; i < OPERATORY.length; i ++)
   {
       if(OPERATORY[i].equals("+")) PLUS = i;
       if(OPERATORY[i].equals("-")) MINUS = i;
       if(OPERATORY[i].equals("*")) MULTI = i;        
       if(OPERATORY[i].equals("/")) DIVIDE = i;
   }
 }
}

--
Thanks for help
KR
Stefan Schulz - 28 Jun 2005 16:52 GMT
> Hi,
>
> is it possible to initialize a public static final member of a class in
> the class' constructor? I try to do this and I receive error message:
>
> variable PLUS is declared final; cannot be assigned.

{... code ...]

No, the static finals need to be initialized before that. Why don't you
use a static initializer block (static { /* ... */ }), which would be
called at class load, in which you can initialize your members.

Signature

You can't run away forever,
But there's nothing wrong with getting a good head start.
          --- Jim Steinman, "Rock and Roll Dreams Come Through"
         

Thomas Fritsch - 28 Jun 2005 16:59 GMT
> Hi,
Hi!

> is it possible to initialize a public static final member of a class in
> the class' constructor? I try to do this and I receive error message:
[quoted text clipped - 31 lines]
> Thanks for help
> KR

No, you can't initialize 'static final' things in a constructor or any
non-static method. But you can (and probably want to) initialize them in
the class initializer:

  static
  {
    for (int i = 0; i < OPERATORY.length; i ++)
    {
        if(OPERATORY[i].equals("+")) PLUS = i;
        if(OPERATORY[i].equals("-")) MINUS = i;
        if(OPERATORY[i].equals("*")) MULTI = i;
        if(OPERATORY[i].equals("/")) DIVIDE = i;
    }
  }

Hence, there is no need for your constructor anymore.

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Chris Smith - 28 Jun 2005 17:17 GMT
> No, you can't initialize 'static final' things in a constructor or any
> non-static method. But you can (and probably want to) initialize them in
[quoted text clipped - 12 lines]
>
> Hence, there is no need for your constructor anymore.

Unfortunately, this still does not work.  The variables PLUS, MINUS,
MULTI, and DIVIDE are blank finals, and there are two rules that apply
here:

1. They must be guaranteed to be initialized by the time static
initialization is complete.

2. They must not be multiply assigned.

Both rules are applied according to the compiler's conservative
data/control flow analysis, as specified in the JLS.  The actual
contents of the array are not considered in performing that analysis.  
So the loop through the OPERATORY array is not a valid way of
accomplishing the initialization.

It's trivial to modify this code to use temporary variables, in order to
work around both restrictions on the direct use of finals.  Here is the
result:

public class Stale
{
   public static final String[] OPERATORS = new String[] {
       "+", "-", "*", "/"
   };

   public final static int PLUS;
   public final static int MULTI;
   public final static int MINUS;
   public final static int DIVIDE;

   static
   {
       int plus = -1, minus = -1, multi = -1, divide = -1;

       for (int i = 0; i < OPERATORS.length; i++)
       {
           if (OPERATORS[i].equals("+")) plus = i;
           if (OPERATORS[i].equals("-")) minus = i;
           if (OPERATORS[i].equals("*")) multi = i;
           if (OPERATORS[i].equals("/")) divide = i;
       }

       assert plus != -1;
       assert minus != -1;
       assert multi != -1;
       assert divide != -1;

       PLUS = plus;
       MINUS = plus;
       MULTI = plus;
       DIVIDE = plus;
   }
}

(Incidentally, I also removed the pointless use of "new String", and
fixed the apparent typo with the array name.)

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Chris Smith - 28 Jun 2005 17:49 GMT
>         PLUS = plus;
>         MINUS = plus;
>         MULTI = plus;
>         DIVIDE = plus;

Oops.  Obviously, that should be:

       PLUS = plus;
       MINUS = minus;
       MULTI = multi;
       DIVIDE = divide;

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Roedy Green - 29 Aug 2005 08:11 GMT
>No, you can't initialize 'static final' things in a constructor or any
>non-static method. But you can (and probably want to) initialize them in
>the class initializer:

The reason is a constructor will be called many times, at least
potentially, but a static initialiser block is guaranteed to be called
exactly once.

Java does not want to deal with the possibility of you changing your
mind about a final value.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Paul - 28 Jun 2005 20:13 GMT
> Hi,
>
> is it possible to initialize a public static final member of a class in
> the class' constructor? I try to do this and I receive error message:
>
> variable PLUS is declared final; cannot be assigned.

<snip>

I would suggest using an enum with a string embedded in each instance if you
are running Java 1.5
--Paul


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.