Working with Java source 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.

If you run into any issues when using the features below, you can contact us by clicking the Report an issue button below.

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.

Folding regions allows you to fold or unfold code snippet to better view the source code.

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.

Peek Definition

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.

Go to Super Implementation

You can keep track of class implementations and overriding methods by clicking the Go to Super Implementation link on hover.

Spring Navigation

Call Hierarchy

A Call Hierarchy view shows all calls from or to a function and allows you to drill into callers of callers and call of calls. Right-click on a function and select Peek > Peek Call Hierarchy.

Call Hierarchy

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

Spring Navigation

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.

Smart Selection

With Smart Selection (semantic selection), you can expand or shrink the selection range based on the semantic information of the caret position in your source code.

  • To expand the selection, use ⌃⇧⌘→ (Windows, Linux Shift+Alt+Right).
  • To shrink the selection, use ⌃⇧⌘← (Windows, Linux Shift+Alt+Left).

Refactoring and Code Actions

Here we will show you the most used refactoring features for Java in Visual Studio Code, namely rename, move, extract methods and variables and more.


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.


The Move refactoring lets you move packages and classes between the source roots of a project, class members to other classes, and inner classes to upper hierarchy levels.

For example, you can perform the move refactoring on a static method if it is used more in another class than in its own class.

You can also move a class to another package.

And move an inner class to new a new file.

Extract methods and variables

Extract to constant, method, and local variables all come in handy with Java on Visual Studio Code.

After the extraction, you can also perform a rename in the same transaction.

Convert a local variable to a field

When selecting an expression, extract to field.

When selecting a variable declaration, convert the variable to field.


The Inline refactoring lets you reverse the refactoring for a local variable, method, and constant.

Convert to enhanced for-loop

The enhanced for-loop is a popular feature. Its simple structure allows one to simplify code by presenting for-loops that visit each element of an array/collection without explicitly expressing how one goes from element to element.

Generate getters and setters

You can bulk generate getters and setters for all new member variables. If the class has more than one field, the source action will prompt a Quick Pick for you to select the target fields to use to generate the accessor methods.

Resolve ambiguous imports

To deal with ambiguous imports, you now have a dropdown list to pick the right one. The code line with the unresolved type is also presented to you to help you decide.

Override/implement methods

With this source action, all the candidates are presented to you with a checklist. You can then decide what to override or implement.

Generate hashCode() and equals()

hashCode() and equals() can be generated with default implementations. All the non-static member variables are listed, and you can customize the generated code using the check list.

There are two options for you to customize the generated code:

  • If you use Java 7+, you can set java.codeGeneration.hashCodeEquals.useJava7Objects to true to generate shorter code that calls Objects.hash and Objects.equals.
  • You can also set java.codeGeneration.hashCodeEquals.useInstanceof to true to use instanceOf operator to check the object types instead of calling Object.getClass().

Generate toString()

There is a new source action to generate the toString() method. Customization is possible with a check list of all the member variables.

Convert to static imports

Convert static functions calls to static imports.

Generate delegate methods

Generate delegate methods

Generate constructor from super class

Add a constructor from super class.

Invert local variable

This refactoring lets you change the sense of a Boolean variable to the opposite one.

Assign parameter to new field

This source action assigns a parameter to a new field for unused parameter(s) in a constructor.

Fix non-accessible reference

This Quick Fix helps you fix non-accessible reference.

Create non-existing package

When your package name doesn't match the folder name, you have the options to either change the package name in your source code, or move the folder in the file system (even when the destination folder doesn't exist yet).

Other Code Actions supported

The list of Code Actions supported by VS Code keeps growing and only listed the most popular ones above. Other notable supported actions include (but not limited to):

  • Convert anonymous class to lambda expression
  • Convert lambda to anonymous class
  • Convert anonymous class to nested class
  • Create unresolved types
  • Assign statement to new variable/field
  • Add final modifier where possible
  • Remove the final modifier
  • Remove unnecessary cast
  • Remove redundant interfaces
  • Add missing case labels in switch statements
  • Jump to definition on break/continue
  • Correct access to static elements

Code Snippets

Visual Studio Code supports a wide range of popular Java code snippets to make you more productive, such as class/interface, syserr, sysout, if/else, try/catch, static main method. Leveraging information from Java language server, it also provides a preview of the code snippet during the selection.

Code Snippet

Formatting, linting, and code analysis

Language Support for Java(TM) by Red Hat also provides formatting settings. You can export an Eclipse formatter file and then use it for your project in VS Code.

In addition, there are also the Checkstyle for Java and SonarLint extensions, which provide features for live linting and code analysis.


With the Checkstyle for Java extension, you can use either existing checkstyle configurations (Google's or Sun's Check) or your own customized files for your project. When editing a Java file, the extension will check the file format and provide Quick Fixes if possible on the fly.

Set Checkstyle configuration file.

The Checkstyle for Java extension supports live linting.

And batch check.

The Problems panel will open when you click the Checkstyle status icon in the Status bar.

Set Checkstyle configuration file

Set Checkstyle configuration file

  • To set the configuration file, right-click the .xml file and select Set the Checkstyle Configuration File.

  • You can also trigger the command Checkstyle: Set Checkstyle Configuration File to choose the configuration file in the File Explorer. The extension looks for a checkstyle.xml file in your workspace to make Checkstyle configuration easier. You will also see the two built-in configurations:

    • Google's Check
    • Sun's Check
  • Command Checkstyle: Set the Checkstyle Configuration detects potential Checkstyle configuration files and lists them. You can also provide a configuration file by directly writing a URL in the input box.

You can also set the Checkstyle version by using the command Checkstyle: Set the Checkstyle Version.

The command will:

  • List the latest Checkstyle version from the main repo.
  • List all the downloaded versions.
  • List all the supported versions.
  • Mark the currently used version with a check symbol.

In addition, you can also bring any 3rd-party modules for Checkstyle by configuring its path. For example, after using the configuration below, you can add <module name="SingleBreakOrContinueCheck"/> or <module name="com.github.sevntu.checkstyle.checks.naming.SingleBreakOrContinueCheck"/> to checkstyle.xml to leverage those checks.

"java.checkstyle.modules": [ "${workspaceFolder}/src/main/resources/sevntu-checks-1.35.0.jar" ]

Check the style and fix the violations

Fix style violation

  • When editing a Java file, the extension will check the file format and provide Quick Fixes if possible. You can click the lightbulb button in the editor to show the available Quick Fixes.

For more details about Checkstyle for Java, visit its GitHub Repository.


The SonarLint extension lets you detect bugs and vulnerabilities as you write code in VS Code. Java is one of the languages supported, and the extension will run in the background and highlight source code that poses a quality or security concern.

Code Analysis on the fly

Issues are highlighted directly in the editor with hovers to provide detailed explanations.

SonarLint Bug

Issues found in the opened file can also be reviewed through the Problems panel of VS Code. When applicable, secondary code locations are mentioned so you can understand where the issue originates from (for example, the code path that led to a bug).

Rule documentation and remediation guidance

For any issue detected, SonarLint provides full documentation about the rule that was violated, and the coding best practice it relates to. This lets you understand why an issue is raised, and most importantly how to best fix it.

SonarLint Rules

Enabling more quality and security rules

By default, SonarLint provides a wide array of rules to detect bugs and vulnerabilities. More checks can be enabled through the SonarLint Rules view.

SonarLint Activate-Deactivate Rules

For more details about the SonarLint for VS Code extension, visit the SonarLint website.