当前位置:网站首页>Final and static
Final and static
2022-06-25 23:51:00 【Program diary】
final
notice final Think of it ( Three points , data , Method , class ):
- Declare data
- Declaration method
- Declaration class
The following are the specific instructions :
Declare data
Declare data to be constant , Can be a compile-time constant , It can also be a constant that cannot be changed after the runtime is initialized .
For basic types ,final Keep the value constant
For reference types ,final You can make the reference unchanged , You can't reference other objects , But it can be modified , Reference cannot be modified , Its own attribute value can be modified

final Student s = new Student();
s.name = "houge";
s.setName("shuaige");
s.name = "houge";
s.setAge(18);
System.out.println(s.toString()); // Student{name='houge', age='18'}
Declaration method
Method cannot be overridden by subclass ,private Method is implicitly specified as final
Declaration class
Class is not allowed to be inherited
static
notice static Think about ( 6'o clock , Variable , Method , Sentence block , Inner class , Guide pack , The order ):
- Static variables
- Static methods
- Static statement block
- Static inner class
- Static guide package
- Initialization sequence
Static variables
Also known as class variable , This variable belongs to the class , You can access it directly through the class name , All instances share , There is only one copy in memory .
What are instance variables ?
Every time an object is created, an instance variable is generated , Live and die with the instance .
Static methods
Class loading exists , Directly through the class name , Static methods cannot be abstract methods , Only the static fields and static methods of the class can be accessed .
class Solution { Integer a; // Instance variables static Integer b; // Static variables public static void func(){ // a = 10 Instance variables cannot be used , Only static variables can be accessed b = 10; System.out.println(" Static methods , Can be called directly through the class name "); } public static void main(String[] args) { Solution.func(); } }Static statement block
characteristic : Run once during class initialization
class Solution { static { System.out.println(" I am a static statement block !"); } public static void main(String[] args) { Solution solution = new Solution(); Solution solution1 = new Solution(); Solution solution2 = new Solution(); } }result : Only one sentence will be output , Because it will only be executed once
I am a static statement block !Whenever it comes to using this class , The contents of the static statement block will be executed , And execute once , It will not be executed later , For example, call a static method :
class Solution { static { System.out.println(" I am a static statement block !"); } static void func(){ System.out.println(" I'm a static method !"); } public static void main(String[] args) { Solution.func(); // Solution solution = new Solution(); // Solution solution1 = new Solution(); // Solution solution2 = new Solution(); } }Output results :
I am a static statement block ! I'm a static method !Then open the following notes , Or these two sentences , You can test it yourself .
Static inner class
Static inner classes can be used directly new, Non static inner classes need to be created with instances of outer classes .
class Solution { class InnerClass{ } static class StaticInnerClass{ } public static void main(String[] args) { Solution solution = new Solution(); InnerClass innerClass = solution.new InnerClass(); StaticInnerClass staticInnerClass = new StaticInnerClass(); } }Static guide package
When using static variables and methods, you don't need to specify the class name , But the readability is not high .
import static com.xxx.ClassName.*Initialization sequence
Static variables and static statement blocks take precedence over instance variables and normal statement blocks ( Static first ), Both are static and depend on the code order . The constructor will not be executed until all variable statement blocks are executed .
Where there is inheritance :
- Parent class ( Static variables , Static statement block )
- Subclass ( Static variables , Static statement block )
- Parent class ( Instance variables , Ordinary statement block )
- Parent class ( Constructors )
- Subclass ( Instance variables , Ordinary statement block )
- Subclass ( Constructors )
One sentence summary , Static first , The parent class takes precedence .
边栏推荐
猜你喜欢
随机推荐
Compiling protobuf protocol files using makefile in PHP
final和static
keil编译运行错误,缺少error:#5:#includecore_cm3.h_过路老熊_新浪博客
使用百度地图API在地图中设置一个覆盖物(InfoWindow),可自定义窗口内容
Apache doris1.0 cluster setup, load balancing and parameter tuning
Jenkins 发布PHP项目代码
数据同步
动态验证码
Jenkins releases PHP project code
The simplest screen recording to GIF gadget in history, licecap, can be tried if the requirements are not high
达梦数据库修改字段信息采坑记
JS中的原型链面试题--Foo和 getName
CXF
树莓派开机发送热点进行远程登录
STL教程5-STL基本概念及String和vector使用
oracle写一个先插入一条数据,在将该数据中一个字段更新的触发器的坑
DPVS-FullNAT模式keepalived篇
支付宝支付接口沙箱环境测试以及整合到一个ssm电商项目中
用ES5的方式实现const
Database - mongodb









