Java Tutorials - Herong's Tutorial Examples - Version 7.03, by Dr. Herong Yang
Creating a Generic Class
This section describes how to create a generic class by declaring type parameters and using them as 'Type Variable' types within the scope of the class.
When creating a generic class, you need to know how to declare a type parameter and how to use it as a "Type Variable" type:
1. Declaring type parameters - When declaring a generic class, you need to provide type parameter enclosed in angle brackets after the class name. Examples are listed below:
class ObjectRegistry<T> {} class HashMap<K,V> {}
2. Using the type parameter - When a type parameter is declared, it becomes a "Type Variable" reference type that can be used within the scope of the class. Examples are listed below:
class ObjectRegistry<T> { public void register(T item) {} } class Box<T> { private T t; public void set(T t) { this.t = t; } public T get() { return t; } }
In the example above, "T" becomes a special reference type called "Type Variable" type which is used to declare local variables like "t".
Last update: 2014.
Table of Contents
Execution Process, Entry Point, Input and Output
Primitive Data Types and Literals
Bits, Bytes, Bitwise and Shift Operations
Managing Bit Strings in Byte Arrays
Reference Data Types and Variables
StringBuffer - The String Buffer Class
System Properties and Runtime Object Methods
►Generic Classes and Parameterized Types
Using a Generic Class - Example
Creating a Generic Class - Example
Raw Type, Generic Type and Parameterized Type
Parameterized Type and Subtyping
Wildcard Parameterized Type Test
Wildcard Parameterized Subtyping
Wildcard Parameterized Subtyping Example
Generic Methods and Type Inference
Lambda Expressions and Method References
Execution Threads and Multi-Threading Java Programs
ThreadGroup Class and "system" ThreadGroup Tree
Synchronization Technique and Synchronized Code Blocks
Deadlock Condition Example Programs