Display Simple Values and Properties Values of Object in Generic


Create entities package and create new class as below:

Student Entity

Create new java class named Student as below:

package entities;

public class Student {

	private String id;
	private String name;
	private int age;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

Product Entity

Create new java class named Product as below:

package entities;

public class Product {

	private String id;
	private String name;
	private double price;
	private int quantity;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public Product(String id, String name, double price, int quantity) {
		this.id = id;
		this.name = name;
		this.price = price;
		this.quantity = quantity;
	}

	public Product() {
	}

}




Display Class

Create generic package. In this package, create new java class named Display as below:

package generic;

import java.lang.reflect.Field;

public class Display<T> {

	private T t;

	public Display(T t) {
		this.t = t;
	}

	public void show() {
		if (this.t instanceof Integer || this.t instanceof Double || this.t instanceof String
				|| this.t instanceof Boolean) {
			System.out.println(this.t);
		} else {
			try {
				Class classObject = this.t.getClass();
				System.out.println(classObject.getSimpleName() + " Info");
				for (Field field : classObject.getDeclaredFields()) {
					System.out.println("Field Name: " + field.getName());
					field.setAccessible(true);
					System.out.println("Field Value: " + field.get(this.t));
					System.out.println("===========================");
				}
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
	}

}
package generic;

import entities.Employee;
import entities.Product;

public class Main {

	public static void main(String[] args) {

		System.out.println("Simple Values");
		Display<Integer> displayInteger = new Display<Integer>(123);
		displayInteger.show();

		Display<String> displayString = new Display<String>("ABC");
		displayString.show();

		Display<Double> displayDouble = new Display<Double>(5.6);
		displayDouble.show();

		Employee employee = new Employee("st01", "name 1", 20);
		Display<Employee> displayEmployee = new Display<Employee>(employee);
		displayEmployee.show();

		Product product = new Product("p01", "name 2", 4.5, 2);
		Display<Product> displayProduct = new Display<Product>(product);
		displayProduct.show();

	}

}




Simple Values
123
ABC
5.6

Employee Info
Field Name: id
Field Value: st01
===========================
Field Name: name
Field Value: name 1
===========================
Field Name: age
Field Value: 20
===========================

Product Info
Field Name: id
Field Value: p01
===========================
Field Name: name
Field Value: name 2
===========================
Field Name: price
Field Value: 4.5
===========================
Field Name: quantity
Field Value: 2
===========================