日期:2014-05-20  浏览次数:20834 次

有点凌乱,求高手点拨
Java code
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吗?


------解决方案--------------------
1,
main方法没代码,不发生什么,如果main方法里有new whatTable()代码,则发生递归造成内存溢出
为什么递归,LZ自己想想吧

2
不等,1说的递归发生内存溢出暂时不看,我们来分析
a是new出来的一个whatTable对象,a.a也是new出来的对象,两个new出来的对象是不同的对象
同样的,a.i和a.a.i也是不同的两个JButton对象
------解决方案--------------------
static 静态属于类,不属于对象,只加载一次。不会发生递归。所以new出来了就是同一个对象。而a.a指向的就是他自身了,后面他不会指向new操作,也就是没有改变引用。
------解决方案--------------------
对,参考/* same_locals_1_stack_item */

Compiled from "whatTable.java"
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;