经典指数          
原因
2956
浏览数
0
收藏数
 

关于以下程序代码的说明正确的是?
1.   public class HasStatic{
2.     private static int x=100;
3.     public static void main(String args[]){
4.          HasStatic hs1=new HasStatic();
5.          hs1.x++;
6.          HasStatic  hs2=new HasStatic();
7.          hs2.x++;
8.          hs1=new HasStatic();
9.          hs1.x++;
10.        HasStatic.x--;
11.        System.out.println("x="+x);
12.     }
13.   } 

  • 程序通过编译,输出结果为:x=103
  • 10行不能通过编译,因为x是私有静态变量
  • 5行不能通过编译,因为引用了私有静态变量
  • 程序通过编译,输出结果为:x=102

     举报   纠错  
 
切换
1 个答案
main是HasStatic的静态方法,在其内部可直接访问静态变量,不存在因为私有变量不能通过编译的问题;如果在其他类中,包括HasStatic的派生类中,均不能访问其私有静态变量
 
切换
撰写答案
扫描后移动端查看本题