java - Re-Initializing a Variable as a Constant -
java - Re-Initializing a Variable as a Constant -
i new java. want know whether there way re-initialize declared global variable constant later after using variable ... because after doing work declared variable want utilize in switch case statement...
class { int x; int y; public static void main(string args[]) { a = new a(); system.out.println(a.x); a.y = 3; a.x = 5; switch (a.y) { case a.x:// } } }
this illustration need... print statement mentions, need variable x. later want utilize in switch case statement. don't want utilize x after switch case statement... please help me...
i want know whether there way re-initialize declared global variable constant later after using variable...
no, there isn't. (java doesn't have global variables; looking @ code, x
, y
instance info members. closest thing java has global variables public static
[aka "class"] info members in public classes.)
that because after doing work declared variable want utilize in switch case statement...
you can't if case values (a.x
, in case) aren't constants. instead, have utilize if/else if/else
:
class { int x; int y; public static void main(string args[]) { a = new a(); system.out.println(a.x); a.y = 3; if (a.y == a.x) { // ... } else if (a.y == something_else) { // ... } else { // ... } } }
java variables switch-statement constants
Comments
Post a Comment