
java: cannot infer type for local variable myVar
If you get a Compile-Time Error "cannot infer type for local variable" in your Java code, then surely you are using JDK 10 or above and you are using var - local variable type inference in your program,
You will get the inferred type for local variable error when you declare a var local variable but do not initialize it. You need to make sure that you do declaration and initialize together,
Error prone code:package examples;
/**
*
* Code2care.org Java Programs
* Date: 7-Apr-2022
* Ver: v1.0
*
*/
public class VarExampleJava {
public static void main(String[] args) {
//some code
}
public void myMethod() {
var myVar; //CE: Cannot infer type: 'var' on variable without initializer
System.out.println(myVar);
}
}
Error:
/Users/c2c/java_11/src/examples/VarExampleJava.java:10:13
java: cannot infer type for local variable myVar
(cannot use 'var' on variable without initializer)
Solution:
As said before, you need to initialize the var variable,
public void myMethod() {
var myVar = "";
System.out.println(myVar);
}
More Posts related to Java,
- Drop table using Java JDBC Template
- Java - Check if array contains the value
- YAML Parser using Java Jackson Library Example
- Java Jackson ObjectMapper Class with Examples
- Get Client IP address from HTTP Response in Java
- How to Word-Warp Console logs in IntelliJ
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Setting Java_Home Environment variable on Windows Operating System
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Java SE JDBC Select Statement Example
- How to extract Java Jar/War/Ear files in Linux
- java: unclosed string literal [Error]
- [Solution] Exception in thread main java.util.EmptyStackException
- Read YAML file Java Jackson Library
- What Java version is used for Minecraft 1.18
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Program] How to read three different values using Scanner in Java
- Java 8 Predicate Functional Interface Examples
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Convert String Date to Date Object in Java
- Struts 2 Hello World Example in Eclipse
- Read a file using Java 8 Stream
- Java - How to set custom thread name?
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- java: ']' expected error [fixed]
More Posts:
- Install Bash on Alpine Linux - Docker - Docker
- Remove Html head and body tags from ckeditor source - Html
- Installing JD Decompiler plugin in Eclipse IDE - Eclipse
- How to stop disable Facebook video autoplay during scroll - Facebook
- WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip. - PIP
- Programmatically check if Facebook is installed on Android device - Android
- How to create MD5 digest in Notepad++ - NotepadPlusPlus
- [Java] Read a File with UTF-8 Encoding - Java