Static、Final关键字详解
2024-09-07 20:30:56
1.Static
详情见下面代码讲解
点击查看代码
package com.Tang.oop.demo07;
public class Student {
private static int age;//静态变量
private double score;//非静态变量
public void run(){
go();//非静态方法可以调用静态方法
}
public static void go(){
}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(Student.age);//静态变量可以被类中的所有实例去共享
//System.out.println(Student.score);这个就会报错,非静态变量并不能用类名去访问
System.out.println(s1.score);
System.out.println(s1.age);
//run();//在静态方法中不能直接调用非静态方法
new Student().run();//必须得通过new一个对象来调用
}
}
2.代码块(静态,匿名,构造)
点击查看代码
package com.Tang.oop.demo07;
public class Person {
{//这一部分就是匿名代码块
System.out.println("匿名代码块");
}
static{//这一部分就是静态代码块,且只执行一次
System.out.println("静态代码块");
}
public Person() {
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person = new Person();
System.out.println("=============");
Person person1 = new Person();
}
}
运行结果
3.final
final为常量,被其修饰的类就无法被继承,也就是不能有子类当父类Person被final修饰时,子类Student就无法在继承Person如下图
最新文章
- 解读ASP.NET 5 &; MVC6系列(6):Middleware详解
- Virtualbox中不能为虚拟机打开一个新任务的原因及解决方法
- linux集群运维工具:pssh
- spring security method security
- Average Cost (AVCO) Method
- No2_3.接口继承多态_Java学习笔记_多态
- C语言的本质(27)——C语言与汇编之计算机结构
- VS中,无法嵌入互操作类型“……”,请改用适用的接口的解决方法
- 【转】css清除浮动float的三种方法总结,为什么清浮动?浮动会有那些影响?
- windows 时间服务器配置详解
- Centos 03 基础命令
- wordpress 无法发送邮件
- error: The function/feature is not implemented (Odd-size DCT's are not implemented)in function cvDCT.
- 【Java面试题】20 运行时异常和一般异常有何区别
- 一、Android Studio入门——Eclipse快捷键配置
- 使用PowerShell批量解除锁定下载的文件
- servlet 文件下载
- 解决 hybird 应用中重复获取 WebView,导致页面元素无法识别的问题
- 20165219 《Java程序设计》实验三(敏捷开发与XP实践)实验报告
- JS的封装(JS插件的封装)
热门文章
- Java汽车租赁系统[源码+数据库]
- 【Hadoop】9、Sqoop组件
- Windows Terminal无法加载WSL [process exited with code 4294967295 (0xffffffff)]
- k8s client-go源码分析 informer源码分析(2)-初始化与启动分析
- spring boot 默认日志替换为 log4j
- 【算法】快速排序(Quick Sort)(六)
- mysqldump速查手册
- Spring是如何整合JUnit的?JUnit源码关联延伸阅读
- 学习Java的第十七天——大数字运算
- 个人冲刺(一)——体温上报app(二阶段)