【JAVA基础——static关键字】

news2024/10/7 16:16:14

JAVA基础

static关键字


文章目录

  • JAVA基础
    • 概述
    • 静态成员与加载顺序


概述

  • static 变量:static变量由该类的所有对象共享,不需要创建对象也可使用。
  • static 方法:允许直接访问,不需要创建对象也可被调用。如 main 方法。
  • static 初始化块:在创建类的第一个对象前自动执行(先执行静态初始化块,再执行初始化块)。
  • static 内部类:外部类对象共享,只能访问外部类的静态成员。

静态成员(字段和方法)属于类本身,而非类的实例。因此,可以通过类名直接访问和调用静态成员,无需创建对象。静态成员在内存中只有一份拷贝,被所有对象所共享。

另外,静态成员不依赖于对象的创建而存在,所以它们可以在没有创建对象的情况下被访问。然而,静态成员不能访问非静态成员,因为非静态成员需要通过对象才能够访问。

静态成员与加载顺序

public class FatherClass {

    // static
    public static String STATIC_FIELD = "father static field";

    static {
        System.out.println("father static block");
    }


    {
        System.out.println("father non static block");
    }


    public FatherClass() {
        System.out.println("father class construction");
    }

    public static void staticMethod() {
        System.out.println("father static method");
    }

    // 内部类
    public static class StaticInnerClass {
        static {
            System.out.println("FatherClass.StaticInnerClass static block");
        }

        {
            System.out.println("FatherClass.StaticInnerClass non static block");
        }

        public StaticInnerClass() {
            System.out.println("FatherClass.StaticInnerClass class construction");
        }

        public static void fatherStaticInnerClassStaticMethod() {
            System.out.println("FatherClass.StaticInnerClass static method");
        }
    }
}


public class ChildClass extends FatherClass {
    public static String STATIC_FIELD = "child static field";

    static {
        System.out.println("child static block 1");
    }

    {
        System.out.println("child non static block 1");
    }

    static {
        System.out.println("child static block 2");
    }

    {
        System.out.println("child non static block 2");
    }

    public ChildClass() {
        System.out.println("child class construction");
    }

    public static void staticMethod() {
        System.out.println("child static method");
    }

    // 内部类
    public static class StaticInnerClass {

        static {
            System.out.println("ChildClass.StaticInnerClass static block");
        }


        {
            System.out.println("ChildClass.StaticInnerClass non static block");
        }


        public StaticInnerClass() {
            System.out.println("ChildClass.StaticInnerClass class construction");
        }

        public static void childStaticInnerClassStaticMethod() {
            System.out.println("ChildClass.StaticInnerClass static method");
        }

    }

    /**
     * 类初始化
     * 1)静态变量(代码块)static只会初始化(执行)一次。
     * 2)当有父类时,完整的初始化顺序为:父类静态变量(静态代码块)->子类静态变量(静态代码块)->父类非静态变量(非静态代码块) ->父类构造器 ->子类非静态变量(非静态代码块)->子类构造器 。
     * 3)当有多个代码块时,按顺序加载.
     * <p>
     * father static block
     * child static block 1
     * child static block 2
     * father non static block
     * father class construction
     * child non static block 1
     * child non static block 2
     * child class construction
     * test start
     * test end
     */
    @Test
    public void testInit() {
        System.out.println("test start");
        System.out.println("test end");
    }

    /**
     * father static block
     * child static block 1
     * child static block 2
     * father non static block
     * father class construction
     * child non static block 1
     * child non static block 2
     * child class construction
     * test start
     * father non static block
     * father class construction
     * child non static block 1
     * child non static block 2
     * child class construction
     * test end
     */
    @Test
    public void testNewObj() {
        System.out.println("test start");
        ChildClass childClass = new ChildClass();
        System.out.println("test end");
    }

    /**
     * father static block
     * child static block 1
     * child static block 2
     * father non static block
     * father class construction
     * child non static block 1
     * child non static block 2
     * child class construction
     * test start
     * FatherClass.StaticInnerClass static block
     * ChildClass.StaticInnerClass static method
     * test end
     */
    @Test
    public void testFatherStaticClass1() {
        System.out.println("test start");
        FatherClass.StaticInnerClass.fatherStaticInnerClassStaticMethod();
        System.out.println("test end");
    }

    /**
     * father static block
     * child static block 1
     * child static block 2
     * father non static block
     * father class construction
     * child non static block 1
     * child non static block 2
     * child class construction
     * test start
     * FatherClass.StaticInnerClass static block
     * FatherClass.StaticInnerClass non static block
     * FatherClass.StaticInnerClass class construction
     * ChildClass.StaticInnerClass static method
     * test end
     */
    @Test
    public void testFatherStaticClass2() {
        System.out.println("test start");
        FatherClass.StaticInnerClass staticInnerClass = new FatherClass.StaticInnerClass();
        staticInnerClass.fatherStaticInnerClassStaticMethod();
        System.out.println("test end");
    }

    /**
     * father static block
     * child static block 1
     * child static block 2
     * father non static block
     * father class construction
     * child non static block 1
     * child non static block 2
     * child class construction
     * test start
     * ChildClass.StaticInnerClass static block
     * ChildClass.StaticInnerClass static method
     * test end
     */
    @Test
    public void testChildStaticClass1() {
        System.out.println("test start");
        ChildClass.StaticInnerClass.childStaticInnerClassStaticMethod();
        System.out.println("test end");
    }

    /**
     * father static block
     * child static block 1
     * child static block 2
     * father non static block
     * father class construction
     * child non static block 1
     * child non static block 2
     * child class construction
     * test start
     * ChildClass.StaticInnerClass static block
     * ChildClass.StaticInnerClass non static block
     * ChildClass.StaticInnerClass class construction
     * ChildClass.StaticInnerClass static method
     * test end
     */
    @Test
    public void testChildStaticClass2() {
        System.out.println("test start");
        StaticInnerClass staticInnerClass = new StaticInnerClass();
        staticInnerClass.childStaticInnerClassStaticMethod();
        System.out.println("test end");
    }
}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/939223.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

华为OD机试 - 求字符串中所有整数的最小和 - 逻辑分析(Java 2023 B卷 100分)

目录 专栏导读一、题目描述二、输入描述三、输出描述四、解题思路五、Java算法源码六、效果展示1、输入2、输出3、说明 华为OD机试 2023B卷题库疯狂收录中&#xff0c;刷题点这里 专栏导读 本专栏收录于《华为OD机试&#xff08;JAVA&#xff09;真题&#xff08;A卷B卷&#…

一个免费好用的全域数据集成平台

文章目录 全域数据集成平台RestCloud一、产品架构一、数据源管理二、离线数据集成三、实时数据集成四、监控中心五、对比总结六、离线数据集成实战1.新建mysql数据源2.离线数据集成3.执行同步数据 大家好&#xff0c;我是脚丫先生 (o^^o) 小伙伴们都知道&#xff0c;在之前数据…

无涯教程-机器学习 - 矩阵图函数

相关性是有关两个变量之间变化的指示&#xff0c;在前面的章节中&#xff0c;无涯教程讨论了Pearson的相关系数以及相关的重要性&#xff0c;可以绘制相关矩阵以显示哪个变量相对于另一个变量具有较高或较低的相关性。 在以下示例中&#xff0c;Python脚本将为Pima印度糖尿病数…

VMware虚拟机的安装以及安装CentOS系统

VMware虚拟机是一款非常受欢迎的虚拟化软件&#xff0c;它可以模拟一台计算机运行在另一台计算机上&#xff0c;从而实现在一台物理机器上运行多个虚拟机的目的。在本文中&#xff0c;我们将会介绍如何安装VMware虚拟机并在其中安装CentOS操作系统。 文章目录 &#x1f4c0;VMw…

Linux用户与组管理(03)(八)

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 目录 前言 一、组管理 1、概述 2、用户信息查看 总结 前言 今天是学习用户与组管理的最后一节课&#xff0c;这节课主要是组管理的内容&#xff0c;希望能一起学习&#xff…

跨足多领域:人脸美颜SDK在医疗、娱乐和安全中的应用案例

随着科技的不断发展&#xff0c;人脸美颜技术不再局限于满足用户的审美需求&#xff0c;而是在医疗、娱乐和安全领域展现出了广泛的应用前景。本文将深入探讨人脸美颜SDK 在这三个领域中的创新应用案例&#xff0c;展示其在不同场景中的独特价值和潜力。 一、医疗领域 1、皮…

Leetcode86. 分隔链表

给你一个链表的头节点 head 和一个特定值 x &#xff0c;请你对链表进行分隔&#xff0c;使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。 你应当 保留 两个分区中每个节点的初始相对位置。 力扣&#xff08;LeetCode&#xff09;官网 - 全球极客挚爱的技术成长平台…

2.3 【MySQL】命令行和配置文件中启动选项的区别

在命令行上指定的绝大部分启动选项都可以放到配置文件中&#xff0c;但是有一些选项是专门为命令行设计的&#xff0c;比方说defaults-extra-file 、 defaults-file 这样的选项本身就是为了指定配置文件路径的&#xff0c;再放在配置文件中使用就没啥意义了。 如果同一个启动选…

winpe还原windows系统备份

准备工作 用大白菜制作一个启动u盘&#xff0c;里面可以镜系统备份文件 插入电脑&#xff0c;启动&#xff0c;按f11&#xff08;这个快捷键因电脑而异&#xff09;&#xff0c;选择启动u盘&#xff0c;进入winpe 硬盘格式化 选择分区助手软件 选择硬盘&#xff0c;右键选择【…

Spring Bean对象生命周期

文章目录 前言基础通俗理解bean作用域 前言 最近学习spring的一些基础概念&#xff0c;所以就先了解了bean对象的概念&#xff0c;而且发现这个里面涉及到很多的内容&#xff0c;比如在spring中一个bean对象是如何创建以及销毁的这些概念&#xff0c;所以就打算总结一些spring…

Spring MVC 学习总结

学习目标 了解 Spring MVC 是什么&#xff0c;为什么要使用它或者说它能解决什么问题&#xff0c;其与 Spring 是什么关系。理解为什么配置 Spring MVC 的前端控制器的映射路径为 “/” 会导致静态资源访问不了&#xff0c;掌握怎么处理这个问题。掌握基于注解方式使用 Spring…

分享一个vue-slot插槽使用场景

需求再现 <el-table-column align"center" label"状态" prop"mitStatus" show-overflow-tooltip />在这里&#xff0c;我想对于状态进行一个三目判断&#xff0c;如果为0那就是进行中&#xff0c;否则就是已完成&#xff0c;期初我是这样写…

九大常见数据结构

常用的数据结构可根据数据访问的特点分为线性结构和非线性结构。线性结构包括常见的链表、栈、队列等&#xff0c;非线性结构包括树、图等。 1 数组 数组可以说是最基本最常见的数据结构。数组一般用来存储相同类型的数据&#xff0c;可通过数组名和下标进行数据的访问和更新。…

self instruct 技术

《SELF-INSTRUCT: Aligning Language Model with Self Generated Instructions》 github: self-instruct 背景 大模型表现惊人&#xff0c;但是严重依赖于人工编写的指令数据。本文中提出self-instruct框架&#xff1a;一种基于大模型自动生成指令数据的方法。 主要步骤 1. …

[ DPU / SmartNIC/ 网卡 ]系统级的测试验证

开局一张图&#xff0c;其他慢慢来编 信雅纳DPU测试解决方案荣获第三届DPU峰会的“匠芯技术奖” 看完这张&#xff0c;小编再送一张&#xff1a;&#xff1a;&#xff1a; 网卡进化 更智能的加速卡SmartNIC/DPU 例如&#xff0c;将部分网络协议处理的功能卸载到 DPU 网卡&am…

Flutter(九)Flutter动画简介

1.动画简介 Animation、Curve、Controller、Tween这四个角色&#xff0c;它们一起配合来完成一个完整动画 Animation Animation是抽象类&#xff0c;和UI渲染没有关系&#xff0c;功能是保存动画的插值和状态&#xff1b;比较常用的是Animation addListener&#xff1a;帧监听…

EXCEL中点击单元格,所在行和列都改变颜色

1、打开VBA编辑环境。 2、选中需要添加程序的Sheet页面。 3、粘贴如下代码在编辑区域并保存后关闭。 Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range) On Error Resume Next Cells.FormatConditions.Delete // 如下代码是行变&#xff0c;在粘贴到VBA中时…

【项目经理】如何说话有条理

如何说话有条理 1. PREP法则2. SCRTV模型3. FFC赞美法则4. RIDE 说服法则 1. PREP法则 2. SCRTV模型 3. FFC赞美法则 4. RIDE 说服法则

WiFi标签工作状态描述

1. LED 灯闪烁代表意义 蓝灯慢闪&#xff08;每隔 500ms 亮一次&#xff09;&#xff1a;标签进入到配置模式 蓝灯快闪&#xff08;每隔 100ms 亮一次&#xff09;&#xff1a; WIFI-TOOL 工具连接上了标签 蓝灯超快闪烁&#xff08;每隔 50ms 闪烁一次&#xff09;&…

每天一分享#读up有感#$记忆宫殿$

记忆宫殿&#xff0c;分享一位喜欢的up&#xff0c;粗略记录下今日鉴赏小结。 【记忆宫殿背句子-哔哩哔哩】 https://b23.tv/vzSCsek 所得 人的记忆就像水波&#xff0c;你只要记住一个中心它会自动往外扩散。 解惑了我记忆时先找关键字加顺序背诵的原理&#xff0c;只是up厉…