nested multiline comments in java

I'm presuming you are wanting to comment out nested code for debugging or testing purposes, right? Because leaving huge blocks of commented out code in production code is generally considered very bad style.

Nested comments is not natively a feature of java. So what can you do? Here are some different options:

Slash Slash comment nesting The best I've been able to come up with is to use an editor that has a hotkey to slashslash comment out an entire block of code as //'s can be nested. Eclipse and Netbeans both have this support, though there are minor differences in how it works between IDEs, as far as when it decides to toggle the comments vs nest them. Usually nesting // comments can be done so long as the selections are not identical.

third party preprocessor That said, J2ME uses a pre-processor in eclipse, but I don't think that would help with desktop java. There do appear to be other preprocessors written for or compatible with eclipse if you search for them. However, that definitely would hinder portability as that's not a standard java feature.

modify comment Another thing I've often done for a quick nested comment need (moreso in css which doesn't do // comments than java though) is to add a space between the final two characters ending the inner comment */ so that java will not end the outer comment at the inner comment's end. Not too difficult (but slightly manual) to restore the original comment. The nested /* is usually ignored by the parser based on how it does its pattern matching. Eg /* code /* nested comment * / more code */`

use version control You could also easily use most any version control system to save a copy of your code with the code to remove. And then later use the version control restore/diff/merge to get your removed code back. Generally this solution is considered the best style. Especially if this commented out code will be needed to be left that way for long amounts of time.

if (false) If your code that you want to mass prevent from running both compiles and is within a single function, you can easily disable it by adding an if statement around it that the condition will never be true such as if (false) { ... } Obviously, this would never pass any sort of lint type code inspection tool, but its quick and easy ;-)


Comments are not meant for leaving dead code - they are meant to describe live code.

For the rare cases that this is needed, using an IDE to comment the lines that you want (CTRL + / in eclipse) is sufficient.

If this is a common practice, rethink your process and your code. And feel free to delete code and commit it to SCM. It won't get lost if you need it.


You could just to if(false){}. Java doesn't really percompile the way C or C++ does but the Java compiler should be smart enough to skip that entire section (once it's determined that this if statement will never evaluate to true). This will only work to comment out code within functions.

Or, if you have a good IDE like eclipse or notepad++ you can use the built-in shortcut keys to comment out multiple lines using //.

Eclipse = ctrl + / (repress to uncomment) N++ = ctrl + k (shift + crtl + k to uncomment)

Tags:

Java

Comments