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);
}
}
Have Questions? Post them here!
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- How to turn off CR LF CRLF in Notepad++ - NotepadPlusPlus
- [fix] Docker Desktop App not starting on Mac (macOS) - Docker
- Java JDBC with Join Queries Example - Java
- Safari Full Screen Shortcut using Keyboard [macOS] - MacOS
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server - Java
- Hyperlink in html (anchor tag) without a underline - Html
- How to Update Microsoft Teams to the latest Version - Teams
- Ways to Show Git Logs in better way using pretty formatting - Git