Get all fields/data members of class in java (example)

Given a class in java containing public,private & protected fields. Get/list/print all fields of class using class Class. We will use getDeclaredFields() of Class to retrieve all data members. 1. Program: print/list all fields/data members of class in java (example) package org.learn.classes; import java.lang.reflect.Field; class Student { private String name; private int standard; protected String age; public… Read More »

Print/list all methods/functions of class in java (example)

Given a class in java containing public,private & protected methods. Get/list/print all methods of class using class Class. We will use getDeclaredMethods() of Class to retrieve all methods. 1. Program: print/list all methods/functions of class in java (example) package org.learn.classes; import java.lang.reflect.Method; class Person { private String name; private String age; private float salary; public Person()… Read More »

Print/list public/protected/private constructors of class in java (example)

Given a class in java containing public,private & protected contructors. Get/list/print all constructors (prive,protected & public) of a class using Class. getConstructors method of Class will list all public constructors of given class. getDeclaredConstructors method of Class will list all private, protected & public constructors 1. Print/list public/protected/private constructors of class in java (example) package… Read More »

Print/list public methods/functions of class in java (example)

Given a class in java containing public,private & protected methods. Get/list/print public methods of class using class Class. We will use getMethods() of Class to retrieve public methods. 1. Program: print/list public methods/functions of class in java (example) package org.learn.classes; import java.lang.reflect.Method; class University { private String name; private String location; private float area; private… Read More »

find out the class name of an object at runtime (java/example)

Given an instance of class in java. We would like to find out the class name of an object. We will use class Class to get name of class at runtime. Class class has method getName, which returns the name of the entity (class, interface, array class, primitive type, or void). 1. Program: find out… Read More »

Create object/instance of class by name using Class.forName in java (examples)

Given an application in java. We would like to create the instance of classes by specifying class name. Instances of the class Class represent classes and interfaces in a running Java application. We will get the reference of loaded class using Class.forName(“Complete path of a class”) And then, we can create instance of loaded class. Class Class does… Read More »

Page 1 of 4312345...102030...Last »