Tuesday, August 17, 2021

How to comment uncomment single line and block of code in Java - Eclipse Tips Tricks Example

Knowing Eclipse shortcut to comment and uncomment single line or block of code can save you a lot of time while working in Eclipse. In fact I highly recommend every Java programmer to go through these top 30 Eclipse shortcut and  10 java debugging tips in Eclipse for improved productivity.  If you are Java programmer, coding in Eclipse and want to comment and uncomment single line or block of code quickly you can either use ctrl + / to comment a single line and subsequently uncomment a commented line. If you want to comment a block of code or a complete method, you have two options you can either line comment (//) all those selected lines or block comment (/* */) those lines.

For line comment multiple lines, select multiple lines and press Ctrl + /, it will put // in front of every line. For block commenting in Eclipse, first, select all the lines or code block you want to comment and then enter ctrl+shift+/ it will put /* on the first line and */ on the last line. 

Btw, if you are a beginner, I suggest you first go through a beginner course like Eclipse Tutorials for Beginners to understand the core concepts of Eclipse IDE and get yourself familiar with UI and essential features. Learning plugins will be a lot easier after that.

Here is a better-illustrated example of commenting and uncommenting Java code in Eclipse IDE :

Eclipse shortcut to comment one line or block code in Java

Suppose I have the following method :

public void printHello(){
    System.out.println("HelloWorld");
}



and I want to comment on a line that is printing HelloWorld, just put the courser on that line and type ctrl+/ , you will see the following line :

public void printHello(){
//    System.out.println("HelloWorld");
}

How to comment uncomment java code in Eclipse IDEIf you want to uncomment that same line, keep the cursor on the same line and type again ctrl+/, it will uncomment that line.

If you want to comment on the whole method, first select the whole method and then type ctrl+/, this will line comment all selected lines as shown below :

//public void printHello(){
//    System.out.println("HelloWorld");
//}

If you want to block comment whole block than type ctrl + shift + / and it will put  /* and */ on first and last line, as shown below :

/*public void printHello(){
    System.out.println("HelloWorld");
}*/

This Eclipse shortcut for commenting and uncommenting Java code will surely help you save few keystrokes and improve your speed while working in Eclipse. To learn more Eclipse comments short cuts see Top 30 Eclipse Keyboard Shortcuts for Java programmers.


Other Eclipse tutorials for Java programmer

2 comments:

  1. I Am Facing A Very Unique Problem Rare For Any To Face When I Am Compiling And Running My Java Code Its Getting Compiled And Shows Output In Text-pad/ Eclipse Editor But When I Am Running It Through saved in a textpad calling in DOS Of Win XP Professional. It Compiling Successfully But When Trying To Run It Though Window's DOS It Throws _______My Code States Like This_______
    ________________________________________________
    public class HelloWorld {

    public static void main(String[] args[]) {
    System.out.println("Hello, World");
    }

    }
    ________________________________________________

    Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
    Caused by: java.lang.ClassNotFoundException: HelloWorld
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

    ReplyDelete
  2. Ranjeet,

    The java classpath is not set correctly when you are trying to run your java program directly on the command line, hence the self-evident 'NoClassDefFoundError'.

    ReplyDelete