自学内容网 自学内容网

笔记-static关键字

1.static关键字内存说明

在这里插入图片描述

2.访问特点

在这里插入图片描述

package com.test.Statics2;

import com.test.statics.Student;

public class Test {
    public static void main(String[] args) {
        // 静态成员中访问非静态成员
        // method3() // 错误-不能直接调用,需要new对象调用
        Test test01 = new Test();
        test01.method3();
    }

    public static void method1() {
        // 静态成员 访问 静态成员
        // 同类,直接调用、不同类,类名调用
        method2();
        Student.drink();

    }

    public static void method2() {

    }

    public void method3() {
        // 非静态成员 访问 静态成员
        // 同类 - 直接调用,new对象调用都可
        method1();
        Test test02 = new Test();
        test02.method3();
        // 不同类 - 类名调用
        Student.drink();
    }

    public void method04(){
        // 非静态成员 访问 非静态成员
        // 同类直接调用
        method3();
        // 不同类new对象调用
        new Person().eat();
    }
}

PS:
1. 不管在不在同一个类中,非静态成员都可以new对象调用
2. 不管在不在同一个类中,静态成员都可以类名调用
3. 能直接调用的时候直接调用


原文地址:https://blog.csdn.net/qq_42866164/article/details/143031083

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!