Creating Javap Tool

| next>><<prevCreating a program that works as javap toolLast Updated : 17 Mar 2025Following methods ofjava.lang.Classclass can be used to display the metadata of a class.MethodDescriptionpublic Field getDeclaredFieldsthrows SecurityExceptionreturns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.public Constructor getDeclaredConstructorsthrows SecurityExceptionreturns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object.public Method getDeclaredMethodsthrows SecurityExceptionreturns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object.Example of creating javap toolLet's create a program that works like javap tool.import java.lang.reflect.*;

public class MyJavap{

public static void main(String args)throws Exception {

Class c=Class.forName(args[0]);

System.out.println("Fields........");

Field f=c.getDeclaredFields;

for(int i=0;i<f.length;i++)

System.out.println(f[i]);

System.out.println("Constructors........");

Constructor con=c.getDeclaredConstructors;

for(int i=0;i<con.length;i++)

System.out.println(con[i]);

System.out.println("Methods........");

Method m=c.getDeclaredMethods;

for(int i=0;i<m.length;i++)

System.out.println(m[i]);

}

}At runtime, you can get the details of any class, it may be user-defined or pre-defined class.Output:Next TopicCreating Appletviewer Tool<<prevnext>> | Method | Description | public Field getDeclaredFieldsthrows SecurityException | returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. | public Constructor getDeclaredConstructorsthrows SecurityException | returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object. | public Method getDeclaredMethodsthrows SecurityException | returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object. |

Method Description
public Field[] getDeclaredFields()throws SecurityException returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.
public Constructor[] getDeclaredConstructors()throws SecurityException returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object.
public Method[] getDeclaredMethods()throws SecurityException returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object.

The metadata of a class can be accessed using the following methods from the java.lang.Class class:

  • getModifiers: Retrieves the modifiers for the class.
  • getName: Returns the name of the entity represented by the class object.
  • getSimpleName: Obtains the simple name of the class.
  • getPackage: Retrieves the package of the class.
  • getSuperclass: Returns the superclass of the class.
  • getInterfaces: Retrieves the interfaces implemented by the class.
Method Description
public Field[] getDeclaredFields()throws SecurityException returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.
public Constructor[] getDeclaredConstructors()throws SecurityException returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object.
public Method[] getDeclaredMethods()throws SecurityException returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object.

Example of creating javap tool

We will now develop a software application that functions similarly to the javap utility tool.

Example

import java.lang.reflect.*;

public class MyJavap{
   public static void main(String[] args)throws Exception {
	Class c=Class.forName(args[0]);
	
	System.out.println("Fields........");
	Field f[]=c.getDeclaredFields();
	for(int i=0;i<f.length;i++)
		System.out.println(f[i]);
	
	System.out.println("Constructors........");
	Constructor con[]=c.getDeclaredConstructors();
	for(int i=0;i<con.length;i++)
		System.out.println(con[i]);
	
        System.out.println("Methods........");
	Method m[]=c.getDeclaredMethods();
	for(int i=0;i<m.length;i++)
		System.out.println(m[i]);
   }
}

During program execution, it is possible to retrieve information about any class, regardless of whether it is a class defined by the user or a built-in class.

Output:

Input Required

This code uses input(). Please provide values below: