What is the IntelliJ shortcut to create a local variable?
Categories:
Mastering Local Variable Creation in IntelliJ IDEA
Learn the essential keyboard shortcuts and techniques to quickly create local variables in IntelliJ IDEA, boosting your coding efficiency.
IntelliJ IDEA is renowned for its powerful code assistance features, and efficiently creating local variables is a cornerstone of productive development. Instead of manually typing out variable declarations, IntelliJ provides smart shortcuts that can infer types and suggest names, saving you valuable time and reducing errors. This article will guide you through the primary methods for generating local variables, from basic extraction to more advanced scenarios.
The 'Extract Variable' Shortcut: Ctrl+Alt+V (Windows/Linux) / Cmd+Option+V (macOS)
The most common and versatile way to create a local variable from an existing expression is using the 'Extract Variable' refactoring. This shortcut allows you to select any expression in your code, and IntelliJ will automatically create a new local variable, assign the expression's value to it, and replace the original expression with the new variable name. It's incredibly useful for breaking down complex lines of code into more readable, manageable parts.
public class Example {
public void calculate() {
// Original code
System.out.println("The result is: " + (10 * 5 + 2));
// After applying Ctrl+Alt+V on '10 * 5 + 2'
int result = 10 * 5 + 2;
System.out.println("The result is: " + result);
}
}
Example of extracting an expression into a local variable.
flowchart TD A[Select Expression] --> B{"Press Ctrl+Alt+V (Cmd+Option+V)"} B --> C{IntelliJ Suggests Type & Name} C --> D[Confirm/Edit Variable Name] D --> E[Local Variable Created] E --> F[Original Expression Replaced] F --> G[Code Refactored]
Flowchart of the 'Extract Variable' refactoring process.
Creating a Variable from a Method Call or Constructor
When you call a method that returns a value, or instantiate an object using a constructor, IntelliJ can automatically create a local variable to hold the result. This is often done by simply typing the method call or constructor, and then using a quick-fix or a specific shortcut.
public class AnotherExample {
public String generateString() {
return "Hello World";
}
public void usage() {
// Type 'generateString().' and press Alt+Enter
String myString = generateString();
// Type 'new StringBuilder().' and press Alt+Enter
StringBuilder builder = new StringBuilder();
}
}
Creating local variables from method calls and constructors using Alt+Enter.
1. Step 1: Type the Expression
Write the method call or constructor invocation that returns a value, e.g., someObject.someMethod()
or new MyClass()
.
2. Step 2: Invoke Quick-Fix
Place your cursor on the expression and press Alt+Enter
(Windows/Linux) or Option+Enter
(macOS).
3. Step 3: Select 'Introduce local variable'
From the context menu that appears, select the option 'Introduce local variable'. IntelliJ will then suggest a variable name and type, which you can accept or modify.
Using Postfix Completion for Variable Creation
IntelliJ's Postfix Completion is another powerful feature that can significantly speed up variable creation. Instead of extracting an expression, you type the expression first, then append a postfix template like .var
to wrap it in a variable declaration.
public class PostfixExample {
public int calculateValue() {
return 100;
}
public void demo() {
// Type 'calculateValue().var' and press Tab/Enter
int i = calculateValue();
// Type 'new ArrayList<String>().var' and press Tab/Enter
java.util.ArrayList<String> strings = new java.util.ArrayList<String>();
}
}
Using the .var
postfix completion to create local variables.
.for
, .if
, .notnull
, etc., by typing an expression followed by a dot and then invoking code completion (Ctrl+Space / Cmd+Space).