
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create User-Defined JavaP Tool
At times, we need information related to a class file. In such a case, we can use the javap tool provided by the Java Development Kit (JDK). We can get more information related to the methods, constructors, and fields present in the class. The purpose of the javap tool is to disassemble one or more class files. It is also known as Java Class File Disassembler. Using the javap tool, we can get more information about the bytecode information about that particular class. The output may vary depending on the options used.
Syntax
The syntax of javap is as follows
javap [options] [classes]
Explanation
The javap keyword is used at the start followed by the options command. Options are an optional parameter. Options can be one or more than one and are separated by space. These are a few widely used options in Java.
-help: It is used to display help for javap menu.
-version: It is used to print the release information
-l: It is used to display line numbers and local variables
-public: It is used to show the public classes and public members
-protected: It is used to show the protected classes and private members
-c: It is used to disassemble the code
-sysinfo: It is used to show the system information
Example
In order to understand how the javap tool works, let us execute the command on the command prompt.
javap java.util.Scanner
Here, we have used the javap command to disassemble the Scanner class in the java.util package. The following image shows the output of the above code. It shows the class modifiers, public methods, and detailed information about the same. One pre-requisite is to have JDK installed for javap tool to work on command prompt.
Output
User-Defined Javap Tool
Now that we know we can use the javap tool on pre-built classes, it is also possible to use the javap tool on user-defined classes.
Steps
Create a user-defined class. Add a constructor and methods to it.
In the Main class, obtain the class instance associated with the custom class. We use the Class.forName()method.
Then the class fields of the custom class are displayed. The getDeclaredFields() method is used to return an array of Field objects. All the public, private, protected, and default fields are included.
Class constructors are shown using the getDeclaredConstructors() method we get an array of constructors defined in the custom class. All the public, private, protected, and default constructors are included.
All the class methods are obtained using the getDeclaredMethods() method that returns an array of methods.
Example
Following is an example to create a user-defined javap tool
import java.lang.reflect.*; // class to use custom javap tool class Customclass{ int x; float y; String z; // class constructor Customclass() { x = 19; y=89.78894532f; z="javap"; } //class methods void printData() { System.out.println(x); System.out.println(y); System.out.println(z); } } //custom javap tool public class Main { public static void main(String[] args) throws Exception { Class cn = Class.forName("Customclass"); //display the fields System.out.println("Class fields"); Field[] field= cn.getDeclaredFields(); for (int i=0;i<field.length;i++) { System.out.println(field[i]); } System.out.println("\n"); //display the constructor System.out.println("Class constructors"); Constructor[] cs= cn.getDeclaredConstructors(); for (int i=0;i<cs.length;i++) { System.out.println(cs[i]); } System.out.println("\n"); //display the methods System.out.println("Class methods"); Method[] m= cn.getDeclaredMethods(); for (int i=0;i<m.length;i++) System.out.println(m[i]); } }
Output
Following is an output of the above code
Class fields int Customclass.x float Customclass.y java.lang.String Customclass.z Class constructors Customclass() Class methods void Customclass.printData()
Explanation
We have obtained the class fields, constructors, and methods using the methods mentioned before. Since our class had 3 fields- int, float and String variables, they got printed. There is a single constructor and a single method.