How Can Pass Generic
Class To A Method In Java?
Hello Readers, In this
short tutorial we are going to learn how can i pass generic class to a method
in java.
Now the question is,
where it can be useful, let assume a task where you have to get all class
fields name which is passed as an argument to a method.
But the question is what
will be the type of that class which is passed as an argument and the answer
is we don't know. In this type of case generic class type would be
helpful. Let see below example.
Create Alien.java class
with some attributes, in this case are id, name and surname.
public class Alien {
private int id;
private String name;
private String surname;
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to
set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the surname
*/
public String getSurname() {
return surname;
}
/**
* @param surname the surname
to set
*/
public void setSurname(String surname) {
this.surname = surname;
}
@Override
public String toString() {
return "Alien [id=" + id + ", name=" + name + ", surname=" + surname + "]";
}
}
Create Alien2.java class
with some attributes, in this case are id, name and surname.
public class Alien2 {
private int id;
private String name;
private String surname;
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to
set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the surname
*/
public String getSurname() {
return surname;
}
/**
* @param surname the surname
to set
*/
public void setSurname(String surname) {
this.surname = surname;
}
@Override
public String toString() {
return "Alien2 [id=" + id + ", name=" + name + ", surname=" + surname + "]";
}
}
Now create TestApp.java
class and create getFieldName method to get class field name which is passed as
an argument.
package com.Pojo;
import java.lang.reflect.Field;
public class TestApp {
public <T> String[]
getFieldNames(Class<T> clazz) throws InstantiationException,
IllegalAccessException {
Field[] allFields = clazz.getDeclaredFields();
String[] fields = new String[allFields.length];
int i = 0;
System.out.println(clazz.getName());
System.out.println("--------------");
for (Field f : allFields) {
fields[i] = f.getName();
i++;
}
return fields;
}
public static void main(String[] args) {
TestApp obj = new TestApp();
try {
String[] f = obj.getFieldNames(Alien.class);
for (String s : f) {
System.out.println(s);
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
When you execute the
main class then you should get the below output. Here i have passed Alien.class
as an argument so the output will be.
com.Pojo.Alien
--------------
id
name
surname
Similarly when i pass
Alien2.class as an argument then the output should be.
com.Pojo.Alien2
--------------
id
name
surname
You can see the
difference between first and second out put where i am printing class name of
the type which is passed as an argument, Good Luck.
You May Like Also
No comments