By Alvin Alexander. Last updated: January 15, 2021
As I was just reminded, Java’s javap command doesn’t show private members by default. You have to use the -p option of javap to see private members.
I was just reminded of that when using the Scala REPL. Given this Person class with a private constructor field named name:
class Person(private var name: String)
javap without the -p option shows this:
scala> :javap -c Person
Compiled from "<console>"
public class $line3.$read$$iw$$iw$Person {
public $line3.$read$$iw$$iw$Person(java.lang.String);
Code:
0: aload_0
1: aload_1
2: putfield #18 // Field name:Ljava/lang/String;
5: aload_0
6: invokespecial #27 // Method java/lang/Object."<init>":()V
9: return
}
But javap -p shows the private fields related to name, which I’ve made bold below:
scala> :javap -p Person
Compiled from "<console>"
public class $line3.$read$$iw$$iw$Person {
private java.lang.String name;
private java.lang.String name();
private void name_$eq(java.lang.String);
public $line3.$read$$iw$$iw$Person(java.lang.String);
}
As shown, this includes the private name field, the getter method named name() and the setter method that shows up with the funky name name_$eq.
javap help output
While I’m in the neighborhood, here’s the full javap help output, circa January, 2021:
$ javap -h
Usage: javap <options> <classes>
where possible options include:
-? -h --help -help Print this help message
-version Version information
-v -verbose Print additional information
-l Print line number and local variable tables
-public Show only public classes and members
-protected Show protected/public classes and members
-package Show package/protected/public classes
and members (default)
-p -private Show all classes and members
-c Disassemble the code
-s Print internal type signatures
-sysinfo Show system info (path, size, date, MD5 hash)
of class being processed
-constants Show final constants
--module <module>, -m <module> Specify module containing classes to be disassembled
--module-path <path> Specify where to find application modules
--system <jdk> Specify where to find system modules
--class-path <path> Specify where to find user class files
-classpath <path> Specify where to find user class files
-cp <path> Specify where to find user class files
-bootclasspath <path> Override location of bootstrap class files
GNU-style options may use = instead of whitespace to separate the name of an option
from its value.
Each class to be shown may be specified by a filename, a URL, or by its fully
qualified class name. Examples:
path/to/MyClass.class
jar:file:///path/to/MyJar.jar!/mypkg/MyClass.class
java.lang.Object