You are on page 1of 3

Java Programming

8-2: Java Class File


Practice Solutions

Lesson Objectives:
• Understand the class file structure
• Identify the Access field
• Identify the Method structure and bytecode
• Method Info: Code_attribute
• Code Attribute: LineNumberTable_attribute
• Class Attribute: SourceFile_attribute

Vocabulary:

Identify the vocabulary word for each definition below.

Class file Contains one Java type, either a class or an interface

Constant pool A table structure in a java class representing various constants

Method info A table in a Class file that describes the method in the class or inter-face

Try It/Solve It:

JDB

1. Write the following program.

public class EmptyCode{

int test=100;

2. Compile the EmptyCode class.

javac EmptyCode

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
3. Specify the number of constant pool entries in the EmptyCode class file.

Solution 1: There are 16 entries in the constant pool of the EmptyCode class file.

#1 = Methodref #4.#13 // java/lang/Object."<init>":()V

#2 = Fieldref #3.#14 // EmptyCode.test:I

#3 = Class #15 // EmptyCode

#4 = Class #16 // java/lang/Object

#5 = Utf8 test

#6 = Utf8 I

#7 = Utf8 <init>

#8 = Utf8 ()V

#9 = Utf8 Code

#10 = Utf8 LineNumberTable

#11 = Utf8 SourceFile

#12 = Utf8 EmptyCode.java

#13 = NameAndType #7:#8 // "<init>":()V

#14 = NameAndType #5:#6 // test:I

#15 = Utf8 EmptyCode

#16 = Utf8 java/lang/Object

Solution 2:

From the Editor, locate the constant_pool_count

4. Determine how many field definitions are in the EmptyCode class file.

There is only one field definition.

5. Determine many method definitions are in the EmptyCode class file.

There is only one method definition.

6. What is the constructor name for the EmptyCode class?

<init>

7. What is the bytecode for the super() statement in the EmptyCode class?

0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

2
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

You might also like