How can i pass generic class to a method in java

Share:
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> clazzthrows 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

You May Like Also

Programming Knowledge


Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database