- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is it possible to instantiate Type-parameter in Java?
Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.
Example
class Student<T> { T age; Student(T age){ this.age = age; } public void display() { System.out.println("Value of age: "+this.age); } } public class GenericsExample { public static void main(String args[]) { Student<Float> std1 = new Student<Float>(25.5f); std1.display(); Student<String> std2 = new Student<String>("25"); std2.display(); Student<Integer> std3 = new Student<Integer>(25); std3.display(); } }
Output
Value of age: 25.5 Value of age: 25 Value of age: 25
Instantiating the type parameter
The parameter we use to represent type of the object is known as type parameter. In short, the parameter we declare at the class declaration in between <>. In the above example T is the type parameter.
Since the type parameter not class or, array, You cannot instantiate it. If you try to do so, a compile time error will be generated.
Example
In the following Java example we have created a class of generic type named Student where, T is the generic parameter later in the program we are trying to instantiate this parameter using the new keyword.
class Student<T>{ T age; Student(T age){ this.age = age; } public void display() { System.out.println("Value of age: "+this.age); } } public class GenericsExample { public static void main(String args[]) { Student<Float> std1 = new Student<Float>(25.5f); std1.display(); T obj = new T(); } }
Compile time error
On compiling, the above program generates the following error.
GenericsExample.java:15: error: cannot find symbol T obj = new T(); ^ symbol: class T location: class GenericsExample GenericsExample.java:15: error: cannot find symbol T obj = new T(); ^ symbol: class T location: class GenericsExample 2 errors
- Related Articles
- Is it possible to synchronize the string type in Java?
- Is it mandatory to use T for type-parameter, while defining Generics classes/methods in Java?
- Is it possible to change the value of the function parameter in JavaScript?
- Is it possible to create a new data type in JavaScript?
- Is it possible to create static constructor in java?
- Is it possible to assign a reference to "this" in java?
- Get the declared method by name and parameter type in Java
- Is it possible to validate the size and type of input=file in HTML5?
- How to instantiate member inner class in Java?\n
- Is it possible to use this keyword in static context in java?
- Where exactly Type-Parameter need to be specified, while declaring generic method in Java?
- What is final parameter in Java
- Is it possible to resume java execution after exception occurs?
- Is it possible to override toString method in an array using Java?
- Is it possible to change directory by using File object in Java?
