简体   繁体   中英

Re-Initializing a Variable as a Constant

I am new to Java. I want to know whether there is a way to re-initialize a declared global variable as a constant later after using it as a variable ... That is because after doing some work with a declared variable I want to use it in a Switch case statement...

class A {
    int x;
    int y;
    public static void main(String args[]) {
        A a = new A();

        System.out.println(a.x);
        a.y = 3;
        a.x = 5;
        switch (a.y) {
            case a.x:// Something
        }
    }
}

this is an example for What I need... print statement mentions, that I need to do something with variable x. later I want to use it in a Switch case statement. I don't want to use x after switch case statement... Please help me...

I want to know whether there is a way to re-initialize a declared global variable as a constant later after using it as a variable...

No, there isn't. (Java also doesn't have global variables; looking at your code, x and y are instance data members . The closest thing Java has to global variables are public static [aka "class"] data members in public classes.)

That is because after doing some work with a declared variable I want to use it in a Switch case statement...

You can't if the case values ( ax , in your case) aren't constants. Instead, you have to use if/else if/else :

class A {
    int x;
    int y;
    public static void main(String args[]) {
        A a = new A();

        System.out.println(a.x);
        a.y = 3;

        if (a.y == a.x) {
            // ...
        }
        else if (a.y == something_else) {
            // ...
        }
        else {
            // ...
        }
    }
}

No, the answer is you cannot do this in Java.

The "things" after case need to be constants.
In Java 7 or later, you can also use strings as case labels.

Switch statement Java

That is not possible. To use something as a case in a switch , it must be a constant known at compile time. If you want to set it based on a variable (ie at run time), there is no way it could be known at compile time.

You can only switch on compile time constants. I think what you're trying to achieve could be done with a simple if statement.

if (a.y == a.x) {
    // do stuff.
}

Since constants in Java are compile time, "re-initializing" does not make sense as a concept.

In Java, when you write a switch statement, the compiler must be able to tell at compile time what the values of the case choices will be. If you want to set up a switch where one of the case choices will be a value determined at runtime--sorry, you can't. Use if statements instead.

Traditionally, switch statements (and CASE statements in other languages) were used to set up jump tables. That is, in a C program:

switch (n) {
    case 1:
        ...code
    case 2:
        ...code
    case 14:
        ...code
}

The compiler would set up a table with at least 14 entries. The #1, #2, and #14 entries in the table would contain code addresses of places that it would jump to. (The others would have the address of a "default" location.) Then, the code generated by the compiler wouldn't be a series of "compare-to-1, if equal-then else compare-to-2, if equal-then else ..." kinds of instructions; instead, after making sure n was in the range of the table, it would index into the table, find the address, and jump directly there. Very efficient. Java does the same thing if you give it an integer (for String s, it can create a jump table based on the hash code, but it still has to compare the strings for equality--thanks to @ErwinBolwidt for providing more information on this).

The point behind all this, though, is that in order to generate the table, the compiler has to know the values ahead of time. So what you're asking for isn't supported, at least in C or Java. (I think some languages do relax this and allow variable values in case , but they may all or mostly be interpreted languages.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM