【JavaSE】static的用法

目录

引入static

1.static修饰成员变量

2.static修饰成员方法

练习

1.阅读如下代码。 请问,对语句行 test.hello(). 描述正确的有()

 2.如下代码的输出结果是什么?

3.当你编译和运行下面的代码时,会出现下面选项中的哪种情况?


引入static

当我们将一个学生类实例化为三个对象s1, s2 ,s3 时,每个学生都有自己的姓名,年龄,性别等信息。

public class Student {
    public String name;
    public int age;
    public String gender;
    //...

    public static void main(String[] args) {
        Student s1 = new Student("s", 22, "女");
        Student s2 = new Student("w", 23, "男");
        Student s3 = new Student("g", 21, "女");
        
        
        
    }
}

假如这三个学生是同一个教室上课,那么教室这个属性就不需要每个学生对象都存储一份,而是要所有的学生对象来共享。这个时候就需要用到static。被static修饰的成员变量和方法,称为静态成员变量(类变量)和静态成员方法(类方法)。

1.static修饰成员变量

静态成员变量的特点:不属于某个具体的对象,而是所有对象共享的。静态成员变量有两种访问方式。1.直接通过类名访问。2.通过对象访问。

public class Student {
    public String name;
    public int age;
    public String gender;
    public static String classRoom = "笃信楼105";
    
    //...


    public static void main(String[] args) {
        //1.静态成员变量可以直接通过类名访问:
        System.out.println(Student.classRoom);
        
        //2.静态成员变量可以通过对象访问:
        Student s1 = new Student("s", 22, "女");
        Student s2 = new Student("w", 23, "男");
        Student s3 = new Student("g", 21, "女");
        System.out.println(s1.classRoom);
        System.out.println(s2.classRoom);
        System.out.println(s3.classRoom);



    }
}

2.static修饰成员方法

static修饰的成员方法是类的方法,而不是某个对象所特有的,静态成员一般通过静态方法访问。静态成员方法有两种访问方式。1.直接通过类名.方法名(...)访问。2.通过对象访问。

public class Student {
    //...
    public static String classRoom = "笃信楼105";

    public static String getClassRoom(){
        return classRoom;
    }

    public static void main(String[] args) {
        System.out.println(Student.getClassRoom());
    }
}

注意:

1.不能在静态方法中访问任何非静态成员变量和非静态成员方法。

2.静态方法中不能出现this。

练习

1.阅读如下代码。 请问,对语句行 test.hello(). 描述正确的有()

package NowCoder;
class Test {
	public static void hello() {
	    System.out.println("hello");
	}
}
public class MyApplication {
	public static void main(String[] args) {
		Test test=null;
		test.hello();
	}
}

A.能编译通过,并正确运行

B.因为使用了未初始化的变量,所以不能编译通过

C.以错误的方式访问了静态方法

D.能编译通过,但因变量为null,不能正常运行

答案:A

解析:

hello方法是一个静态方法,静态方法不依赖于对象,所以指不指向对象没有关系。(Test test = null表示不指向对象)。还可以直接通过类名调用hello方法:Test.hello();

public class MyApplication {
    class Test{
        public static void hello(){
            System.out.println("hello");

        }
    }
    public static void main(String[] args) {
        Test test = null;//表示不指向对象
        test.hello();//通过对象调用静态方法
        Test.hello();//直接通过类名调用静态方法

    }
}

但当hello方法不再是静态方法时,就会报空指针异常的错:

 

 2.如下代码的输出结果是什么?

public class Test { 
    public int aMethod(){
        static int i = 0;
        i++; 
        return i;
    } 
public static void main(String args[]){
    Test test = new Test(); 
    test.aMethod(); 
    int j = test.aMethod();
    System.out.println(j);
    } 
}

A.0

B.1

C.2

D.编译失败

答案:D

解析:

static只能修饰成员变量(也就是在aMethod方法外,Test类里的变量),不能修饰局部变量。

3.当你编译和运行下面的代码时,会出现下面选项中的哪种情况?

public class Pvf{
    static boolean Paddy;
    public static void main(String args[]){
        System.out.println(Paddy);
    }
}

A.编译时错误

B.编译通过并输出结果false

C.编译通过并输出结果true

D.编译通过并输出结果null

答案:B

解析:成员变量没有赋初值的时候,会有默认初始值。如:int是0,boolean是false, float是0.0f, char是'/u0000', 引用类型是null,如String.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值