Sure, you can edit out anything irrelevant that you come across, whether in a question or answer, whether old or new.
The catch is, it actually needs to be irrelevant, and you need to be certain that it is irrelevant. And I'd go so far as to say that most of the time, that is not going to be the case.
For prose, it is a bit easier to make this determination of irrelevance. Everyone knows you can edit out greetings, salutations, platitudes, and other meaningless ramblings. But for code, it is a bit more difficult to decide what is irrelevant. You have to be a language expert, and you probably have to be certain of what the answer is before you can decide.
In this particular case, I'm hesitant to say for certain since I'm not a Java language expert, but I don't think the code is irrelevant. As others have said, it is important to provide context. That small snippet in isolation may not be enough to understand how the code in question works. I'd certainly err on the side of leaving it be.
I do agree with giusti, though, that you could optimize the presentation of the question by reordering the code blocks. Put the important stuff first, and the optional context second. This addresses your concerns about simplicity and making it easier to understand what the question is asking. There will still be a need to scroll, but scrolling isn't that difficult if you know that you can skip the rest. A big block of code is honestly pretty easy to ignore, if you decide you don't need to study it.