Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #2566

Re: Enums: Properties vs. Methods

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.programmer
Subject Re: Enums: Properties vs. Methods
Date 2011-03-29 19:40 -0400
Organization albasani.net
Message-ID <imtqlu$u35$1@news.albasani.net> (permalink)
References (1 earlier) <h0e4p693s0ioq69bttb88usgq757b7v5eu@4ax.com> <imtgcu$u0g$1@dont-email.me> <imthnl$ckb$1@news.albasani.net> <imting$pcc$1@dont-email.me> <imtojc$q0t$2@news.albasani.net>

Show all headers | View raw


Lew wrote:
> Daniele Futtorovic wrote:
>> For all purposes, yeah. I would however expect the one with the constant to be
>> marginally easier, as it requires no analysis of the field.

> Which of the two are you calling "the one with the constant"?

The reason for my confusion:

 From the JLS, §17.5.3:
"If a final field is initialized to a compile-time constant ... uses of that 
final field are replaced at compile time with the compile-time constant."

Details in §15.28:
"A compile-time constant expression is an expression denoting a value of 
primitive type or a String that does not complete abruptly and is composed 
using only the following:
  . . .

  o Simple names that refer to constant variables (§4.12.4).

§4.12.4:
"We call a variable, of primitive type or type String, that is final and 
initialized with a compile-time constant expression (§15.28) a /constant 
variable/."

<sscce type="com.lewscanon.eegee.Finality">

package com.lewscanon.eegee;

import java.util.Random;

/**
  * Finality.
  */
public class Finality implements Runnable
{
   final Random randy = new Random();

   final int kount = 3;

   @Override
   public void run()
   {
     int value = randy.nextInt( kount + 1 );
     System.out.print( " value = "+ value +": " );

     switch ( value )
     {
     default:
       System.out.println( "MISS" );
       break;

     case kount:
       System.out.println( "HIT!" );
       break;
     }
   }

   /**
    * main.
    * @param args String [].
    */
   public static void main( String [] args )
   {
     Finality fina = new Finality();

     for ( int ix = 0; ix < 10; ++ix )
     {
       System.out.print( ix + "." );
       fina.run();
     }
   }
}
</sscce>

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-03-29 08:18 -0700
  Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-29 11:47 -0400
    Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-03-30 20:00 +0200
      Re: Enums: Properties vs. Methods Lew <lew@lewscanon.com> - 2011-03-30 13:17 -0700
        Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-03-31 00:48 -0700
          Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-31 08:41 -0400
            Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-03-31 08:26 -0700
              Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-31 22:16 -0400
                Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-04-02 11:56 +0200
                Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-04-02 10:29 -0400
                Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-04-03 13:14 +0200
  Re: Enums: Properties vs. Methods markspace <-@.> - 2011-03-29 09:26 -0700
    Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-03-30 20:06 +0200
      Re: Enums: Properties vs. Methods markspace <-@.> - 2011-03-30 12:40 -0700
        Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-03-31 00:38 -0700
  Re: Enums: Properties vs. Methods Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-03-29 19:03 +0200
    Re: Enums: Properties vs. Methods Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-03-29 19:04 +0200
  Re: Enums: Properties vs. Methods Roedy Green <see_website@mindprod.com.invalid> - 2011-03-29 12:49 -0700
  Re: Enums: Properties vs. Methods Roedy Green <see_website@mindprod.com.invalid> - 2011-03-29 12:52 -0700
    Re: Enums: Properties vs. Methods Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-03-29 22:45 +0200
      Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-29 17:08 -0400
        Re: Enums: Properties vs. Methods Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-03-29 23:25 +0200
          Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-29 19:05 -0400
            Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-29 19:40 -0400
              Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-03-31 02:57 -0700
                Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-31 08:52 -0400
                Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-31 08:54 -0400
            Re: Enums: Properties vs. Methods Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-03-30 18:40 +0200
              Re: Enums: Properties vs. Methods Lew <lew@lewscanon.com> - 2011-03-30 10:33 -0700
                Re: Enums: Properties vs. Methods Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-03-30 20:43 +0200
                Re: Enums: Properties vs. Methods Lew <lew@lewscanon.com> - 2011-03-30 13:20 -0700
                Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-31 08:55 -0400
  Re: Enums: Properties vs. Methods Wanja Gayk <brixomatic@yahoo.com> - 2011-04-02 11:23 +0200
    Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-04-02 10:36 -0400
      Re: Enums: Properties vs. Methods Wanja Gayk <brixomatic@yahoo.com> - 2011-04-02 21:20 +0200

csiph-web