-2

Sometime when we declare and initialize a variable, say we have an int i =10; then after some code this variable would be modified like this code bellow

public class reset {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int i = 10;
        int co = 1;
        while (co < 10) {
            i++;
            System.out.println(i + "*" + co + "=" + i * co);
            if (i == 99) {
                i = 11; //line 11
                co++;
            }
        }
    }
}

then at some point (here at line 11) we need to re-initialize then variable, wouldn't it be nice if we had any language feature doing it automatically instead for example

reset:i

I think it's very beneficial for productivity, isn't it?

Curcuma_
  • 851
  • 2
  • 12
  • 37

4 Answers4

4

we need to re-initialize then variable, wouldn't it be nice if we had any language feature doing it automatically instead?

No

I think it's very beneficial for productivity, isn't it?

No

Resetting a variable to its start value is in many cases a sign that the scope of the variable is to large. So with clean code you hardly ever need such feature.

And of course every feature comes at the cost of complicating the language even more.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
2

The initializer line

int i = 10;

simply creates byte code instructions to assign the value 10 to the variable. That assignment is no different than any other assignment.

To implement reset, there would need to be an extra bit of metadata kept for each variable to say what the special, initial value is.. That metadata is not currently kept in the symbol table, since there is no concept in Java for a 'initial value'. The additional overhead would trade off against the utility of the reset command.

Might be a good idea, but you can get the same thing by just declaring a constant, and reassigning to the constant.

AgilePro
  • 5,588
  • 4
  • 33
  • 56
  • the idea of creating a constant and assigning it every time escaped from my mind, but talking about overhead, why not just go back for first assigning byte code instruction, and do it again, no need for additional data?, am I understanding you? – Curcuma_ Sep 21 '14 at 16:32
  • 1
    You don't want to copy byte code just willy-nilly. You don't want to reuse a variable willy-nilly neither. It would not be very readable if you had to look up that initial value 2 pages of code instead of just seeing the assignment (hopefully to a readable constant identifier). It would break symmetry if this can be done for some assignments (to constant values) and not for assignments with side effects. Not very useful, there are tons of reasons why this is not a good idea, sorry. – Maarten Bodewes Sep 21 '14 at 16:40
  • @initParam - yes if the Java language had a concept of a 'default' value, then you could invent a 'reset' statement, track the default value in the symbol table, and generate the proper byte codes to assign the default value in place of the reset statement. However, I am less and less convinced of the utility of that. I can't think of any particular case of my code where 'reset' would have been any particular simplification of the code. – AgilePro Sep 24 '14 at 21:03
1

What about this code? I can understand your question from a starter's perspective, but usually it requires a bit more practice to see why certain constructs are not required:

public class NoReset {
    private static final int X_START = 11;
    private static final int X_END = 99;
    private static final int Y_START = 1;
    private static final int Y_END = 9;

    public static void main(String[] args) {
        for (int y = Y_START; y <= Y_END; y++) {
            for (int x = X_START; x <= X_END; x++) {
                final int result = x * y;
                System.out.printf("%d * %d = %d%n", x, y, result);
            }
        }
    }
}

Note that you should not nest to many loops, but creating a "hidden loop" is at least as dangerous, it gets very hard to track variables such as i within your code.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
0

If you want to use the current Java language to do what you want to do, then simply provide a function in each class that you want to perform a "reset" like:

private void reset() {
    var = xxx;
    var2 = yyy;
    ...
}
ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37