Java: switch with "goto case ..."

I found myself in need for jump from one case in switch to another case, to avoid code repetition. Chaining the cases without break; was not sufficient since it only allows jump from one case to one imediatelly following and I wanted to jump from multiple to one. I also wasn't able to refactor the code further, since I had already constructed the switch to just populate variables and the logic was outside.

I did some research. I Firstly thought of using goto, which seemed ugly, hard to maintain and soon I realized, that Java has goto as reserved keyword, but it was never implemented into the language, so luckily, this was not a option.

Then I found this topic on StackOverflow, which mentioned combination of switch inside while and use continue to repeat switch match. This idea put me on track. I have slightly improved the examples and created pretty simple and fully working implementation of goto case ... for Java. It goes like this:

// we need variable for switch condition
int jumpTo = someInitialValue;
do {
    switch(jumpTo) {
        case 1:
            // do some action and continue to case 2 without break
        case 2:
            // do more action and exit with break
        case 3:
            // do some action and continue with case 2
            jumpTo = 2;
            // optionally do default action or
            // throw Exception() if you want
            // to pass only defined cases
    // if the switch successfully completes,
    // it breaks while loop here
} while (true);

If you inside switch reassign jumpTo variable to new target case and then call continue;, the switch will run in the loop again and will match the targeted case. The while(true); statement allows infinite iteration, so you can call continue; as many times you need. When your case is final, you end it with break; as usual. This exits switch and right after the switch is another break; which terminates while loop.

This example can also be rewritten using Java labels, but that's only more verbose solution.