import javax.swing.JTable;
public class whatTable extends JTable{
JButton i=new JButton();
whatTable a=new whatTable();
public static void main(String[] args){
}}1.这短代码运行的过程是怎么样的?
2.a.i==a.a.i==a.a.a.i吗?
public class whatTable extends JTable{
JButton i=new JButton();
whatTable a=new whatTable();
public static void main(String[] args){
}}1.这短代码运行的过程是怎么样的?
2.a.i==a.a.i==a.a.a.i吗?
main方法没代码,不发生什么,如果main方法里有new whatTable()代码,则发生递归造成内存溢出?
为什么递归,LZ自己想想吧2
不等,1说的递归发生内存溢出暂时不看,我们来分析
a是new出来的一个whatTable对象,a.a也是new出来的对象,两个new出来的对象是不同的对象
同样的,a.i和a.a.i也是不同的两个JButton对象
其实我一开始和你想的差不多。
也就是说,我把它改为
import javax.swing.JButton;
import javax.swing.JTable;
public class whatTable extends JTable{
static JButton i=new JButton();
static whatTable a=new whatTable();
public static void main(String[] args){
a=new whatTable();
System.out.println(a.i==a.a.i&&a.a.i==a.a.a.i);
}}首先,在eclipse运行中,没有发生什么递归。
System.out.println(a.i==a.a.i&&a.a.i==a.a.a.i);的结果是true
我猜想内存是这样的
————
| a |-------
|-------- |
| | |
| |<------
|————
你说对吗,谢谢啦
public class applet.whatTable extends javax.swing.JTable
SourceFile: "whatTable.java"
minor version: 0
major version: 50
Constant pool:
const #1 = class #2; // applet/whatTable
const #2 = Asciz applet/whatTable;
const #3 = class #4; // javax/swing/JTable
const #4 = Asciz javax/swing/JTable;
const #5 = Asciz i;
const #6 = Asciz Ljavax/swing/JButton;;
const #7 = Asciz a;
const #8 = Asciz Lapplet/whatTable;;
const #9 = Asciz <clinit>;
const #10 = Asciz ()V;
const #11 = Asciz Code;
const #12 = class #13; // javax/swing/JButton
const #13 = Asciz javax/swing/JButton;
const #14 = Method #12.#15; // javax/swing/JButton."<init>":()V
const #15 = NameAndType #16:#10;// "<init>":()V
const #16 = Asciz <init>;
const #17 = Field #1.#18; // applet/whatTable.i:Ljavax/swing/JButton;
const #18 = NameAndType #5:#6;// i:Ljavax/swing/JButton;
const #19 = Method #1.#15; // applet/whatTable."<init>":()V
const #20 = Field #1.#21; // applet/whatTable.a:Lapplet/whatTable;
const #21 = NameAndType #7:#8;// a:Lapplet/whatTable;
const #22 = Asciz LineNumberTable;
const #23 = Asciz LocalVariableTable;
const #24 = Method #3.#15; // javax/swing/JTable."<init>":()V
const #25 = Asciz this;
const #26 = Asciz main;
const #27 = Asciz ([Ljava/lang/String;)V;
const #28 = Field #29.#31; // java/lang/System.out:Ljava/io/PrintS
tream;
const #29 = class #30; // java/lang/System
const #30 = Asciz java/lang/System;
const #31 = NameAndType #32:#33;// out:Ljava/io/PrintStream;
const #32 = Asciz out;
const #33 = Asciz Ljava/io/PrintStream;;
const #34 = Method #35.#37; // java/io/PrintStream.println:(Z)V
const #35 = class #36; // java/io/PrintStream
const #36 = Asciz java/io/PrintStream;
const #37 = NameAndType #38:#39;// println:(Z)V
const #38 = Asciz println;
const #39 = Asciz (Z)V;
const #40 = Asciz args;
const #41 = Asciz [Ljava/lang/String;;
const #42 = Asciz StackMapTable;
const #43 = class #41; // "[Ljava/lang/String;"
const #44 = Asciz SourceFile;
const #45 = Asciz whatTable.java;{
static javax.swing.JButton i;static applet.whatTable a;static {};
Code:
Stack=2, Locals=0, Args_size=0
0: new #12; //class javax/swing/JButton
3: dup
4: invokespecial #14; //Method javax/swing/JButton."<init>":()V
7: putstatic #17; //Field i:Ljavax/swing/JButton;
10: new #1; //class applet/whatTable
13: dup
14: invokespecial #19; //Method "<init>":()V
17: putstatic #20; //Field a:Lapplet/whatTable;
20: return
LineNumberTable:
line 7: 0
line 8: 10
line 6: 20public applet.whatTable();
Code:
Stack=1, Locals=1, Args_size=1
0: aload_0
1: invokespecial #24; //Method javax/swing/JTable."<init>":()V
4: return
LineNumberTable:
line 6: 0 LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lapplet/whatTable;
public static void main(java.lang.String[]);
Code:
Stack=3, Locals=1, Args_size=1
0: new #1; //class applet/whatTable
3: dup
4: invokespecial #19; //Method "<init>":()V
7: putstatic #20; //Field a:Lapplet/whatTable;
10: getstatic #28; //Field java/lang/System.out:Ljava/io/PrintStream;
13: getstatic #17; //Field i:Ljavax/swing/JButton;
16: getstatic #20; //Field a:Lapplet/whatTable;
19: pop
20: getstatic #17; //Field i:Ljavax/swing/JButton;
23: if_acmpne 51
26: getstatic #20; //Field a:Lapplet/whatTable;
29: pop
30: getstatic #17; //Field i:Ljavax/swing/JButton;
33: getstatic #20; //Field a:Lapplet/whatTable;
36: pop
37: getstatic #20; //Field a:Lapplet/whatTable;
40: pop
41: getstatic #17; //Field i:Ljavax/swing/JButton;
44: if_acmpne 51
47: iconst_1
48: goto 52
51: iconst_0
52: invokevirtual #34; //Method java/io/PrintStream.println:(Z)V
55: return
LineNumberTable:
line 11: 0
line 12: 10
line 13: 55 LocalVariableTable:
Start Length Slot Name Signature
0 56 0 args [Ljava/lang/String; StackMapTable: number_of_entries = 2
frame_type = 115 /* same_locals_1_stack_item */
stack = [ class java/io/PrintStream ]
frame_type = 255 /* full_frame */
offset_delta = 0
locals = [ class "[Ljava/lang/String;" ]
stack = [ class java/io/PrintStream, int ]
}
E:\wap\mail\bin>javap -verbose applet.Test
Compiled from "Test.java"
public class applet.Test extends java.lang.Object
SourceFile: "Test.java"
minor version: 0
major version: 50
Constant pool:
const #1 = class #2; // applet/Test
const #2 = Asciz applet/Test;
const #3 = class #4; // java/lang/Object
const #4 = Asciz java/lang/Object;
const #5 = Asciz <init>;
const #6 = Asciz ()V;
const #7 = Asciz Code;
const #8 = Method #3.#9; // java/lang/Object."<init>":()V
const #9 = NameAndType #5:#6;// "<init>":()V
const #10 = Asciz LineNumberTable;
const #11 = Asciz LocalVariableTable;
const #12 = Asciz this;
const #13 = Asciz Lapplet/Test;;
const #14 = Asciz main;
const #15 = Asciz ([Ljava/lang/String;)V;
const #16 = Method #17.#19; // java/lang/Integer.valueOf:(I)Ljava/l
ang/Integer;
const #17 = class #18; // java/lang/Integer
const #18 = Asciz java/lang/Integer;
const #19 = NameAndType #20:#21;// valueOf:(I)Ljava/lang/Integer;
const #20 = Asciz valueOf;
const #21 = Asciz (I)Ljava/lang/Integer;;
const #22 = Asciz args;
const #23 = Asciz [Ljava/lang/String;;
const #24 = Asciz i;
const #25 = Asciz Ljava/lang/Integer;;
const #26 = Asciz SourceFile;
const #27 = Asciz Test.java;{
public applet.Test();
Code:
Stack=1, Locals=1, Args_size=1
0: aload_0
1: invokespecial #8; //Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 3: 0 LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lapplet/Test;
public static void main(java.lang.String[]);
Code:
Stack=1, Locals=2, Args_size=1
0: iconst_0
1: invokestatic #16; //Method java/lang/Integer.valueOf:(I)Ljava/lang/In
teger;
4: astore_1
5: return
LineNumberTable:
line 10: 0
line 11: 5 LocalVariableTable:
Start Length Slot Name Signature
0 6 0 args [Ljava/lang/String;
5 1 1 i Ljava/lang/Integer;
}