Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.
See all our reports

Try JRebel 6 beta: Adding new instance fields without restarting

In our previous posts on some of the long-awaited, upcoming features in JRebel 6 (coming later in 2014), we described how you will now be able to make changes to not only class, resource and framework files, but also IMPLEMENTED INTERFACES (something that wasn’t possible before) and CLASS HIERARCHY. Interested in trying out JRebel 6 beta? Simply download the latest version of JRebel and then follow the instructions here for enabling JRebel 6 beta functionality (like changing Superclasses or implemented interfaces without restarting)!

Instance fields largely represent the state of the class during execution. When reloading classes, the general approach taken by JRebel is to preserve the state of the existing class instances. This allows the user to stay in the flow without having to recreate the state to exercise the changes that were introduced to the application.

To keep the state intact, JRebel doesn’t rerun the class constructors. The decision was made with a tradeoff – the new instance fields added by the user were left uninitialized. Until now, this behavior did well, but has been causing issues to some users. JRebel 6 comes now with a new feature – it can initialize the new instance fields while preserving the existing state of the reloaded class.

Example – Counters

Consider the following example, where the Counters class keeps its state in counter1 field. By running the while loop and incrementing the counter we will monitor the state of the execution.

Counters c = new Counters();
while (true) {

public class Counters {
    int counter1 = 0;
    public void incrementCounters(){ counter1++; }
    public void printCounters(){
        System.out.println("counter1 = " + counter1);

When the program runs, we will add a new instance field – a new counter – but we try to initialize it with some value, i.e.

int counter2 = 10;
public class Counters {
    int counter1 = 0;
    int counter2 = 10;
    public void incrementCounters(){ counter1++; counter2++; }
    public void printCounters(){
        System.out.print("counter1 = " + counter1);
        System.out.println(" counter2 = " + counter2);

JRebel 5

With JRebel 5, the reloaded class state was preserved. The existing field (i.e. counter1) was still incrementing from the correct position after the class was reloaded:

counter1 = 42
counter1 = 43
2014-08-01 10:37:43 JRebel: Reloading class 'com.zt.Counters'.
2014-08-01 10:37:43 JRebel: WARN Class 'com.zt.Counters' has a new non-static field 'counter2', it will be null on existing instances
counter1 = 44 counter2 = 1
counter1 = 45 counter2 = 2

However, counter2 started from 1 instead of 11 as we expected. Also the IDE would notify you about this fact:

JRebel 6

JRebel 6 can now initialize the new fields while preserving the state of the existing instances:

counter1 = 24
counter1 = 25
2014-08-01 10:39:39 JRebel: Reloading class 'com.zt.Counters'.
counter1 = 26 counter2 = 11
counter1 = 27 counter2 = 12
counter1 = 28 counter2 = 13

The new field, counter2, was initialized after the Counters class was reloaded. Great success!

Try JRebel 6 and get this functionality…

Now that we have finally found a solution to this, I invite you to try it out: just enable the superclass reloading option in the IDE plugin settings… the instructions are here.