package org.code2care.generics;
public class Example<T extends Number> {
public static void main(String[] args) {
Example<String> example = new Example<String>();
}
}
Compilation Error:
Example.java:7: error: type argument String is not within bounds of type-variable T
Example example = new Example();
^
where T is a type-variable:
T extends Number declared in class Example
Example.java:7: error: type argument String is not within bounds of type-variable T
Example example = new Example();
^
where T is a type-variable:
T extends Number declared in class Example
2 errors
Here, we are dealing with bounded type generics (T extends Number) in Java.
Why this error?
In our case, we should have type T to be a subtype of Number, or its upper-bound can be Number itself. As String is not a type of Number hence we get this compilation error.
Fix
You have to make sure that the type that you use with extends should be a subtype of the upper-bound specified or the upper-bound class type itself.
So in our case, we have some of the following possibilities.
Client<Byte> byteType = new Client<Byte>();
Client<Short> shortType = new Client<Short>();
Client<Integer> intType = new Client<Integer>();
Client<Long> longType = new Client<Long>();
Client<Double> doubleType = new Client<Double>();
Client<Number> numberType = new Client<Number>();
Have Questions? Post them here!
- Error: Can not find the tag library descriptor for
- Create a Database Table using JDBC PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- Java Jackson with Maven Example
- [fix] Java JDBC ConnectException: Connection refused
- Spring Boot: Transactions Management with JDBCTemplate Example
- Java Get Current Date for a Specific Time Zone
- What are the 8 Primitive Data Types in Java
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Maven Eclipse (M2e) No archetypes currently available
- How to Sort a LinkedList in Java
- [Fatal Error] XML The markup in the document following the root element must be well-formed.
- Split a String in Java with Examples
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- Truncate table using Java JDBC Example
- Java: Generate random numbers within a range
- Parse XML file in Java using DOM Parser
- How to get Client IP address using Java Code Example
- JDBCTemplate Querying Examples with Spring Boot 3
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- String Boot + Redis - SET and GET String Commands Examples
- Setting up Spring Boot 3 + Maven + MySQL + JDBC Example
- Spring Boot: JdbcTemplate Update Query With Parameters Example
- Java Split String by Spaces
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- fill_parent vs match_parent vs wrap_content - Android
- [Java] Bad return type in lambda expression: int cannot be converted to boolean - Java
- Cannot open or preview pdf with view only and restricted download access in Microsoft Teams - Teams
- Zsh Shell: Custom alias that you may find useful - MacOS
- vi undo redo command [Examples] - Mac-OS-X
- [Java] Read a File with UTF-8 Encoding - Java
- Save cURL Command Output to a external file - cURL
- Keyboard shortcut to close tab in Notepad++ - NotepadPlusPlus