Sample code:
1. package org.code2care.generics;
2.
3. public class Sample<T> {
4.
5. public static void main(String... args) {
6. T obj;
7. }
8. }
Now when you complete this code using javac you get the below compliation error.
% Sample.java:6: error: non-static type variable T cannot be referenced from a static context
T obj;
^
1 error
Why this error?
The issue occurs at line number 6 because we are trying to declare a generic T type parameter which is of non-static type inside the main method which is of static type.
Fix:
One of the solutions to this issue can be that you can create a non-static method and then make use of the T type parameter.
package org.code2care.generics;
public class Sample<T> {
public void nonStaticMethod(T t) {
System.out.println(t);
}
public static void main(String[] args) {
System.out.println(10);
}
}
You can download this article in various formats for your convenience. Choose from the options below:
Facing issues? Have Questions? Post them here! I am happy to answer!
- Java Jackson ObjectMapper Class with Examples
- How to add a Delay of a Few Seconds in Java Program
- Java SE JDBC Select Statement Example
- How to Word-Warp Console logs in IntelliJ
- Convert Java Array to ArrayList Code Example
- [Java] Error: Unmappable character for encoding UTF-8. Save could not be completed.
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- How to extract Java Jar/War/Ear files in Linux
- Java: Convert String to Binary
- Java Generics: Type parameter cannot be instantiated directly
- How to Create and Run a Command Line Application using Spring Boot
- How to Convert a Java Object into an Optional with Example
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to Set JAVA_HOME PATH to Mac .zshrc profile file
- Formatting Double in Java [Examples]
- Java 8: Stream map with Code Examples
- Fix: SyntaxError: ( was never closed [Python]
- Java Split String by Spaces
- [fix] Java JDBC SQLSyntaxErrorException: Unknown database
- Fix: SpringFramework BeanDefinitionValidationException: Could not find an init method named
- Java Date Time API: LocalDateTime get(TemporalField field) examples
- How to create Date in yyyy-MM-dd format in Java
- Java: Create Temporary Directory and File and Delete when application terminates
- Fix RabbitMQ: AuthenticationFailureException: ACCESS_REFUSED
- Run Java Code Every Second
- How to Set PowerShell as Default Shell on Windows Terminal - Windows
- incorrect line ending: found carriage return (\r) without corresponding newline (\n) - Android
- Xcode 13 - unknown error compiling for iOS 15.0 but module has a minimum deployment target of iOS 15.2 - iOS
- Display ls command file sizes in KB (kilobytes) MB (megabytes) or GB (gigabytes) [Linux/macOS] - MacOS
- Android: Maps and Places using Maps SDK - Android
- How to Split a String by Space in Rust - Rust
- How to Center Align Image in Bootstrap - CSS
- How to Change Bootstrap Carousel Slide Speed - Bootstrap