0

I've been trying to play around with hotswapping Java code in Eclipse, but I have no idea what the limitations and rules of hotswapping Java code in Eclipse are, so my efforts frequently fail. If anyone could offer a relatively detailed explanation of how to hotswap code in Eclipse or link me to one that would be great.

For instance, this code hotswaps if I change the value of u:

public class apples extends tuna {

    public static void main(String[] args) throws InterruptedException {
        while (true) {
            ddop();
            Thread.sleep(1000);
        }
    }

    public static void ddop() {
        int u = 3;
        System.out.println(u);
    }
}

but this code doesn't:

public class apples extends tuna {
    static int u;
    public static void main(String[] args) throws InterruptedException {
        int u = 3;
        while (true) {
            System.out.println(u);
            Thread.sleep(1000);
        }
    }
}

Could anyone provide an explanation why? And yes, I do have the "build automatically" flag checked and am running in debug mode.

Brian
  • 14,610
  • 7
  • 35
  • 43
pipsqueaker117
  • 2,280
  • 9
  • 35
  • 47

2 Answers2

1

I believe that in order to hot swap a method's source that method must be in the call stack. In your first example the changed method gets invoked repeatedly while in the second example the method is invoked once and only then does the code loop. If the code changes once the method has already run then that method is never in the call stack again and can not pick up the code change.

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
0

I'm not 100% sure about the example above, but in general the rule is that you can't change the interface of a class. For example, adding ore removing methods, changing method signatures, adding/removing static fields, etc.

E-Riz
  • 31,431
  • 9
  • 97
  • 134