Editing Java in Visual Studio Code
Visual Studio Code is a source code editor first and foremost with rich editing features. In this document, we will go through a few Java-specific features, which are helpful when working with Java.
Code editing and navigation
With the Outline view, you can conveniently navigate your methods within the same class. Editing code is also easy with IntelliSense for smart code completions and signature details. You can use code snippets as well as various code actions such as generating Getters/Setters and organizing imports to further boost your productivity.
Java support in Visual Studio Code detects issues within your code automatically, and provides you with Quick Fix suggestions.
As a Java editor, it also supports CodeLens (references) and Javadoc hovers and highlights out of box.
Search for symbols
You can search for symbols in the current file or workspace to navigate your code more quickly.
To search for a symbol in the current file, use Quick Open (⌘P (Windows, Linux Ctrl+P)) then enter the '@' command, then enter the name of the symbol you're looking for. A list of potential matches will appear and be filtered as you type. Choose from the list of matches to navigate to its location.
To search for a symbol in the current workspace, start by pressing ⌘T (Windows, Linux Ctrl+T), then enter the name of the symbol. A list of potential matches will appear as before. If you choose a match that was found in a file that's not already open, the file will be opened before navigating to the match's location. Alternatively, you can also use Quick Open (⌘P (Windows, Linux Ctrl+P)) then enter the '#' command to search the current workspace. ⌘T (Windows, Linux Ctrl+T) is just the shortcut for the '#' commands, so everything works the same.
You can take a quick look at how a symbol was defined by using the Peek Definition feature. This feature displays a few lines of code near the definition inside a peek window, so you can take a look without navigating away from your current location.
To peek at a symbol's definition, place your cursor on the symbol anywhere it's used in your source code and then press ⌥F12 (Windows Alt+F12, Linux Ctrl+Shift+F10). Alternatively, you can choose Peek Definition from the context menu (right-click, then choose Peek Definition).
Go to Definition
You can also quickly navigate to where a symbol is defined by using the Go to Definition feature.
To go to a symbol's definition, place your cursor on the symbol anywhere it is used in your source code and then press F12. Alternatively, you can choose Go to Definition from the context menu (right-click, then choose Go to Definition). When there's only one definition of the symbol, you'll navigate directly to its location, otherwise the competing definitions are displayed in a peek window as described in the previous section and you have to choose the definition that you want to go to.
Navigating code with Spring Boot
Spring Boot Tools extension provides enhanced navigation and code completion support for Spring Boot projects.
@/shows all defined request mappings (mapped path, request method, source location)
@+shows all defined beans (bean name, bean type, source location)
@>shows all functions (prototype implementation)
@shows all Spring annotations in the code
To learn more about Spring Boot support with Visual Studio Code, read Spring Boot in Visual Studio Code.
Code completion in Visual Studio Code for Java is provided by Language Support for Java(TM) by Red Hat. The extension is powered by the same Java development tools (JDT) behind Eclipse, so you can expect the same level of support.
In addition, there's also AI-assisted IntelliSense called IntelliCode. It saves you time by putting what you're most likely to use at the top of your completion list. IntelliCode recommendations are based on thousands of open-source projects on GitHub each with over 100 stars, so it’s trained on the most common usages from high quality projects. When combined with the context of your code, the completion list is tailored to promote those practices. Here's IntelliCode for Java in action.
IntelliCode works well with popular Java libraries and frameworks like Java SE and Spring. It will help you whether you are doing monolithic web apps or modern microservices.
Here we will show you the most used refactoring features for Java in Visual Studio Code, namely rename, extract methods and variables.
Rename allows you to rename variables, classes, methods, packages, folders, and almost any Java identifiers. When you rename an identifier, all references to that identifier are also renamed. The shortcut to invoke the Rename refactoring is F2. When you invoke the shortcut on an identifier in the editor, a small box displays within the editor itself where you can change the identifier name. When you press Enter, all references to that identifier are changed too.
Extract methods and variables
Extract to constant, method, and local variables all come in handy with Java on Visual Studio Code.
Generate getters and setters
You can bulk generate getters and setters for all new member variables.
In addition, there's a Checkstyle for Java extension, which you can use with either existing
checkstyle configurations (Google's or Sun's Check) or your own customized files. When opening or saving a Java file, the extension will check the file format and provide quick fixes if possible.