package org.code2care.generics;
public class Example<T,V> {
public static void main(String[] args) {
System.out.println("Generics Example!");
}
public void method() {
T t = new T();
V v = new V();
}
}
Compilation Error:
Code2care@Mac % javac Example.java
Example.java:10: error: unexpected type
T t = new T();
^
required: class
found: type parameter T
where T is a type-variable:
T extends Object declared in class Example
Example.java:11: error: unexpected type
V v = new V();
^
required: class
found: type parameter V
where V is a type-variable:
V extends Object declared in class Example
2 errors
This is a violation of the rules of dealing with Generic types in Java. You cannot create an instance of generic type parameters.
Hence the compilation error clearly states that it was expecting a class type but a type parameter was provided.
Solution:The ideal way would be to pass the type parameters as parameters to your method.
package org.code2care.generics;
public class Example<K,V> {
public static void main(String[] args) {
Example<String,Integer> example = new Example<>();
example.method("Sam",20);
}
public void method(K k, V v) {
System.out.println(k);
System.out.println(v);
}
}
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- How to minimize all Windows in Mac - MacOS
- Clear SFTP Prompt Screen on macOS Terminal - MacOS
- Get Button Text onClick Android App - Android
- How to Create a Website (Webpage) using HTML on Windows Notepad - Windows
- Open Docker from Terminal Command on Mac - MacOS
- Mac - Open Finder App using Terminal Current Location - MacOS
- Android M cannot run app using play button : Android Studio - Android
- Java: Convert a Text file into Array of Bytes Example - Java