why public static final string can not used in java switch -


java support switch string, in case field, how use constant string? this:

phxlog l = new phxlog();         (string s : l.fields) {         object v = mapbasedata.getdata().get(s);             switch (s) {                 case l.log_field_time:   // wrong, how use constant instead of "xxxx"                     l.settime((string)v);                     break; 

(credit goes ejp's answer)

from jls, mentioned expression put in case needs constant expression.

there different form of constant expression, , string contants in list:

qualified names (§6.5.6.2) of form typename . identifier refer constant variables (§4.12.4).

from code, gives hint l.log_field_time not fulfill requirement, because l not typename (hinted for (string s : l) {). if log_field_time static final field, should use classnameofl.log_field_time instead, , thing should work.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -