A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© rel4x 中级黑马   /  2014-9-29 16:44  /  1369 人查看  /  16 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

[p=169, null, left]public class test{

[p=169, null, left]   static int i;

[p=169, null, left]   public int test(){

[p=169, null, left]    i++;

[p=169, null, left]     return i;

[p=169, null, left]  }

[p=169, null, left]public static void main(String args[]){

[p=169, null, left] Test test=new Test();

[p=169, null, left]  test.test();

[p=169, null, left]   System.out.println(test.test());

[p=169, null, left]  }

[p=169, null, left]}


评分

参与人数 1技术分 +1 收起 理由
敏敏好学 + 1

查看全部评分

16 个回复

正序浏览
rel4x 中级黑马 2014-9-30 13:11:45
17#
张业婷 发表于 2014-9-30 12:30
补充一下
System.out.println(test.i);
因为是的类中的要通过实例名或者类名调用一下才可以! ...

嗯嗯 知道了,多谢了
回复 使用道具 举报
张业婷 发表于 2014-9-30 12:25
test.test();
System.out.println(test.test());
这里出现了两次test.test();就是调用了两次;

补充一下
System.out.println(test.i);
因为是的类中的要通过实例名或者类名调用一下才可以!
回复 使用道具 举报
张业婷 发表于 2014-9-30 12:18
你调用了两次方法进行了两次i++,而i是静态的,就相当给i加了两次1

test.test();
System.out.println(test.test());
这里出现了两次test.test();就是调用了两次;
public class test
{
   static int i;
   public int test()
           {
                         i++;
                        return i;
                }
        public static void main(String args[])
        {
                test test=new test();
                test.test();
                System.out.println(i);
        }
}
在样在运行一下,一定是1。静态变量存放在静态方法区中不会随着方法的结束而消失!它的生命周期
和类一样长,类什么时候消失它就什么时候消失,所以你上面的代码是间接的给i进行了二次叠加
回复 使用道具 举报
rel4x 发表于 2014-9-30 10:57
嘿嘿,学的还不是好咯

你调用了两次方法进行了两次i++,而i是静态的,就相当给i加了两次1
回复 使用道具 举报
rel4x 中级黑马 2014-9-30 10:57:53
13#
张业婷 发表于 2014-9-30 09:21
静态变量int 型的初始值为0.有初始值哦!谁说没有了?

嘿嘿,学的还不是好咯
回复 使用道具 举报
rel4x 中级黑马 2014-9-30 10:56:27
12#
哈达洋 发表于 2014-9-29 21:22
你的i是成员变量,系统为默认为它初始化,int类型默认初始化为0,引用类型默认为null,boolean为false。
...

原来是这样,谢谢 知道了:)
回复 使用道具 举报
静态变量int 型的初始值为0.有初始值哦!谁说没有了?
回复 使用道具 举报
  1. test.test();//调用一次test()方法,i加一次
  2. System.out.println(test.test());//再一次调用test()方法,i再加一次
  3. 所以打印结果是2
复制代码
回复 使用道具 举报
希筱诺 来自手机 中级黑马 2014-9-29 22:45:11
9#
i是共享的
回复 使用道具 举报
路过,飘过,走过
回复 使用道具 举报

  1. public class test
  2. {
  3.    static int i;
  4.    public int test()
  5.            {
  6.                          i++;
  7.                         return i;
  8.                 }
  9.         public static void main(String args[])
  10.         {
  11.                 test test=new test();
  12.                 test.test();
  13.                 System.out.println(test.test());
  14.         }
  15. }
复制代码


我想这才是你的代码吧。成员变量都有默认初始化值,int型默认初始化值是0.代码中有两个test.test()语句(打印语句里那个test.test()也算哦),就是调用了两次test()方法,那自然i的值就是2了。
回复 使用道具 举报 1 0
你的i是成员变量,系统为默认为它初始化,int类型默认初始化为0,引用类型默认为null,boolean为false。
回复 使用道具 举报
clevergump 发表于 2014-9-29 17:06
倒数第三行 test.test();使得 i==1, 倒数第二行中的 test.test()使得 i ==2,由于test.test()的返回值就是 i ...

不是很明白。。。
回复 使用道具 举报
张业婷 发表于 2014-9-29 16:55
因为 i 是静态的属于共享数据,是随着类的加载而加载谁都可以访问它,当你用对象调用方法是调用了两次,值 ...

他不是没有初始值么
回复 使用道具 举报
倒数第三行 test.test();使得 i==1, 倒数第二行中的 test.test()使得 i ==2,由于test.test()的返回值就是 i ,所以就输出2了
回复 使用道具 举报
因为 i 是静态的属于共享数据,是随着类的加载而加载谁都可以访问它,当你用对象调用方法是调用了两次,值当然是2了!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马