今天笔试的题目有点不太懂,有木有人帮我分析下啊

jsshizhanab 2014-05-20 12:52:33

public class Test1 {

public static Test1 t = new Test1();

public static int a;

public static int b = 0;

Test1(){
a++;
b++;
}

public void printAB() {
System.out.println(a + ","+b);
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Test1().printAB();
}

}
...全文
209 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jsshizhanab 2014-05-20
  • 打赏
  • 举报
回复

public class Test1 {
	
	public static Test1 t = new Test1();
	
	public static int a;
	
	public static int b = 0;
	
	Test1(){
		a++;
		b++;
	}
	
	public void printAB() {
		System.out.println(a + ","+b);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Test1().printAB();
	}

}
caoyclearnc 2014-05-20
  • 打赏
  • 举报
回复
我想的和实际机器上运行的结果 命名都是 2,2 不知道楼上几位是怎么想的? 类加载 ,只进行一次。所以类变量的初始化,也只能进行一次。
wf_kingofring 2014-05-20
  • 打赏
  • 举报
回复
引用 8 楼 xuhu_it 的回复:
public class Test
{
    
 
    
    public static int a;
    
    public static int b = 0;
       public static Test t = new Test();
    
    Test(){
        a++;
        b++;
    }
     
    public void printAB() {
        System.out.println(a + ","+b);
    }
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        
        /*
         * 这句话执行的顺序是 1、4、2、3、4
         */
        new Test().printAB();
    }
}
JVM会在类加载完成后,给static变量分配内存地址和初始化默认值(只是初始化默认值,而不是赋值); 当类被首次主动(类似于 创建类的实例,调用类的静态方法)使用时进行初始化,此时的初始化是指按照开发人员定义的值进行赋值 也就是 “=”号右边的值 也就是说 当 第1、4执行完后 a=1,b=1 然后执行2的时候 由于没有=号 所以不会赋值 a还是=1,执行3的时候 将b值赋值为0 所以再 执行4时 a 等于2 b等于 1
换下代码位置试下,又不一样
wf_kingofring 2014-05-20
  • 打赏
  • 举报
回复
引用 2 楼 xiaopeipei2004 的回复:
1 public static Test1 t = new Test1(); 2 public static int a; 3 public static int b = 0; 4 new Test1(); 按照执行顺序: 1 a==1,b==1 2 a==1,b==1 3 a==1,b==0 4 a==2,b==1
编译器运行下……
tony4geek 2014-05-20
  • 打赏
  • 举报
回复
考的加载顺序
xuhu_it 2014-05-20
  • 打赏
  • 举报
回复
public class Test
{
    //1
    public static Test t = new Test();
    //2
    public static int a;
    //3 
    public static int b = 0;
    
    //4
    Test(){
        a++;
        b++;
    }
     
    public void printAB() {
        System.out.println(a + ","+b);
    }
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        
        /*
         * 这句话执行的顺序是 1、4、2、3、4
         */
        new Test().printAB();
    }
}
JVM会在类加载完成后,给static变量分配内存地址和初始化默认值(只是初始化默认值,而不是赋值); 当类被首次主动(类似于 创建类的实例,调用类的静态方法)使用时进行初始化,此时的初始化是指按照开发人员定义的值进行赋值 也就是 “=”号右边的值 也就是说 当 第1、4执行完后 a=1,b=1 然后执行2的时候 由于没有=号 所以不会赋值 a还是=1,执行3的时候 将b值赋值为0 所以再 执行4时 a 等于2 b等于 1
OPPPPOP 2014-05-20
  • 打赏
  • 举报
回复
有人能给出点参考资料么 或者说这是什么原理 非常感谢
zhjdg 2014-05-20
  • 打赏
  • 举报
回复
调试器调一下
public class Test1 {
	
	
	
	public static int a;
	
	public static int b = 0;
	public static Test1 t = new Test1();
	Test1(){
		a++;
		b++;
	}
	
	public void printAB() {
		System.out.println(a + ","+b);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Test1().printAB();
	}

}
haorengoodman 2014-05-20
  • 打赏
  • 举报
回复
2楼正解
引用 4 楼 haorengoodman 的回复:
楼上正解,没什么好说的了, 题目中如果加上 非静态的成员变量 会考的更全一点
haorengoodman 2014-05-20
  • 打赏
  • 举报
回复
楼上正解,没什么好说的了, 题目中如果加上 非静态的成员变量 会考的更全一点
放学有种别走 2014-05-20
  • 打赏
  • 举报
回复
引用 2 楼 xiaopeipei2004 的回复:
1 public static Test1 t = new Test1(); 2 public static int a; 3 public static int b = 0; 4 new Test1(); 按照执行顺序: 1 a==1,b==1 2 a==1,b==1 3 a==1,b==0 4 a==2,b==1
这个题目是在考察类反射机制吗?
grapepaul 2014-05-20
  • 打赏
  • 举报
回复
1 public static Test1 t = new Test1(); 2 public static int a; 3 public static int b = 0; 4 new Test1(); 按照执行顺序: 1 a==1,b==1 2 a==1,b==1 3 a==1,b==0 4 a==2,b==1

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧