概述

static 关键字方便在没有创建对象的情况下来进行调用方法和变量(优先级高于对象),可以用来修饰类的成员方法、类的成员变量,另外可以编写static代码块来优化程序性能

static变量

static变量也称作静态变量,静态变量和非静态变量的区别是:静态变量被所有的对象所共享,在内存中只有一个副本,它当且仅当在类初次加载时会被初始化。

public class PersonDemo {
    public static void main(String[] args) {
        Person person1 = new Person("张三", 16);
        Person person2 = new Person("李四", 17);
        Person person3 = new Person("王五", 18);
        Person person4 = new Person("赵六", 19);

        /*
         * The static field Person.address should be accessed in a static way
         * 静态成员变量应该通过静态的方式访问(注意这里是应该,不是必须)
         * 
         * Change access to static using 'Person' (declaring type)
         * 使用Person声明类型来更改对静态的访问
         * 通过类名来操作成员变量:Person.address
         */
        System.out.println("姓名:" + person1.name + " 年龄:" + person1.age + " 地址:" + Person.address);
        System.out.println("姓名:" + person2.name + " 年龄:" + person2.age + " 地址:" + Person.address);
        System.out.println("姓名:" + person3.name + " 年龄:" + person3.age + " 地址:" + Person.address);
        System.out.println("姓名:" + person4.name + " 年龄:" + person4.age + " 地址:" + Person.address);
    
        // 通过类名直接调用static修饰的成员变量,此时是没有对象的
        System.out.println("没有对象:" + Person.address);
        
        /*
         *  Cannot make a static reference to the non-static field Person.name
         *  
         *  将name添加static后没有报错
         */
//        System.out.println("没有对象:" + Person.name);
        
        /*
         * 通过对象调用statice修饰的成员方法
         * 
         * The static method test() from the type Person should be accessed in a static way
         */
        // person1.testStatic();
        
        // 通过类名直接调用静态方法
        Person.testStatic();
    }
}

总结

1、通过类名调用静态变量,因为静态变量与对象无关
2、静态变量被所有对象共享,一处更改处处更改

static方法

static方法一般称作静态方法,由于静态方法不依赖于任何对象就可以进行访问,因此对于静态方法来说,是没有this的,因为它不依附于任何对象,既然都没有对象,就谈不上this了。并且由于这个特性,在静态方法中不能访问类的非静态成员变量和非静态成员方法,因为非静态成员方法/变量都是必须依赖具体的对象才能够被调用。

public class Person {
    public String name = "张三";
    public int age;
    public static String address = "郑州";

    public Person() {
        super();
    }

    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    
    // 自定义static修饰的成员方法
    public static void testStatic() {
        /*
         * 静态方法不能调用非静态方法
         * Cannot make a static reference to the non-static method test() from the type Person
         */
        // test();
        System.out.println("static mothed");
        
        /*
         * 不能再静态方法中使用this关键字
         * 
         * Cannot use this in a static context
         */
        // this.name;
    }
    
    public void test() {
        System.out.println("method");
    }
}

总结

1、static修饰的方法不能访问本类中的非静态变量和方法,不能使用this
2、通过类名来调用静态方法,工具类的应用很广泛

总结

static修饰的成员变量和方法都是对象所共享的资源,对其进行的操作回作用于所有对象。

static修饰的成员变量和方法依赖于类不依赖于对象,即没有对象

static修饰的成员变量和成员方法都可以通过类名调用

static修饰的成员方法不能调用非static修饰的变量和方法,不能使用this关键字

静态变量常和final关键字搭配作为常量使用,静态方法常用于工具类

最后修改:2021 年 01 月 24 日 12 : 30 PM
如果觉得此文章有用,请随意打赏