当前位置:网站首页>2021-11-05 understand main method syntax, code block and final keyword
2021-11-05 understand main method syntax, code block and final keyword
2022-07-27 21:41:00 【Xiao Tang learns to catch babies】
= = = = = = = One 、 understand main Method syntax = = = = = = = =
One 、 explain main The form of the method :public static void main(String[] args)
1. main Method is called by the virtual machine
2. java The virtual machine needs to call the main() Method , So the access rights of this method must be public
3. java Virtual machine is executing main) Method, you don't have to create an object , So the method must be static
4. This method receives String Array parameters of type , The array saves the execution java The parameters passed to the running class when the command is executed
5. java Program parameters executed 1 Parameters 2 Parameters 3
Two 、 hot tip
1) stay main() In the method , We can call main The static method or static property of the class in which the method is located .
2) however , Non static members in this class cannot be accessed directly , You must create an instance object of the class , This object can be used to access the non static members of the class
= = = = = = = Two 、 Understand code blocks = = = = = = = =
One 、 Basic introduction :
Coded blocks are also called initialization blocks , Belongs to a member of a class [ That is, part of the class , It's similar to the method , Encapsulate logical statements in the method body , adopt {} Surrounded .
But different from the method , No method name , No return , No parameters , Only the method body , And you don't have to call... Explicitly through objects or classes , But when loading classes , Or implicitly call... When creating an object .
Two 、 Basic grammar :
[ Modifier ]{
Code
};
Note description :
1) Modifiers are available , What to write , I can only write static
2) Code blocks fall into two categories , Use static The modification is called static code block , No, static Embellished , It's called a normal code block / Non static code block .
3) A logical statement can be any logical statement ( Input 、 Output 、 Method call 、 loop 、 Judgment, etc )
4); The number can be written , You can omit it .
3、 ... and 、 Basic understanding :
1) Equivalent to another form of constructor ( Complementary mechanisms to constructors ), You can do initialization
2) scene : If there are repeated statements in multiple constructors , It can be extracted into the initialization block , Improve code reuse .
Four 、 Notes and details :
1) static Code blocks are also called static blocks , The function is to initialize the class , And it executes as the class loads , And only once . If it's a normal code block , Every time an object is created , Is executed .
2) When the class is loaded [ Important back !]
① When creating an object instance (new)
② Create subclass object instances , The parent class is also loaded
③ When using static members of a class ( Static attribute , Static methods )
3) Common code block , When creating an object instance , Will be implicitly called .
Created once , It will be called once .
If you only use static members of a class , Ordinary blocks of code do not execute .
Summary :1.static The code block is executed when the class is loaded , Only execute — Time
2. A normal block of code is called when an object is created , To create a , Call once
3. Class loaded 3 In this case , Need to remember .
= = = = = = = 3、 ... and 、final keyword = = = = = = = =
One 、 Basic introduction :
final Modifiable class 、 attribute 、 Methods and local variables .
In some cases , Programmers may have the following requirements , Will be used to final:
1) When you don't want the class to be inherited , It can be used final modification .
2) When you don't want a method of a subclass to be covered by a subclass / rewrite (override) when , It can be used final Keyword modification .
3 When you don't want the value of an attribute of a class to be modified , It can be used final modification .
4) When you don't want a local variable to be modified , have access to final modification
Two 、 Precautions and use details :
1)final Modified attributes are also called constants , It's usually used XX_XX_ XX Named after the
2) final Modified attributes are defined , The initial value must be assigned , And it can't be changed in the future , The assignment can be in one of the following positions 【 Select a position and assign an initial value 】:
① Definition time : Such as public final double TAX_RATE=0.08;
② In the constructor
③ In the code block .
3) If final Decorated attributes are static , Then the initialization position can only be :
① Definition time ② In static code blocks
Cannot assign value in constructor .
4) final Class cannot inherit , But you can instantiate objects .
5) If the class is not final class , But it contains final Method , This method cannot be overridden , But it can be inherited .
边栏推荐
- 枚举和注解
- 内部类(四种内部类详解)
- 一篇文章带你走进pycharm的世界----别再问我pycharm的安装和环境配置了!!!
- Acwing3715. Minimum exchange times (simulation idea of bubble sorting method)
- ACM mm 2022 | Zhejiang University proposed: point cloud segmentation, active learning of new SOTA
- Ziguang zhanrui: dozens of 5g terminals based on chunteng 510 will be commercially available in 2020
- Analysis of STL source code
- 腾讯云[HiFlow】| 自动化 -------HiFlow:还在复制粘贴?
- 技术管理 - 一定要抓大放小
- 美国新宣布制裁的6家中国企业到底是何方神圣?
猜你喜欢

Plato Farm在Elephant Swap上铸造的ePLATO是什么?为何具备高溢价?

LM NAV: robot navigation method based on large models of language, vision and behavior

C language - Introduction - grammar - pointer (12)

How to realize a good knowledge management system?

The solution that the laptop can connect to WiFi but the browser cannot open the web page

30 minutes to thoroughly understand the synchronized lock upgrade process

单核CPU, 1G内存,也能做JVM调优吗?

哈希表的查找与插入及删除

枚举Enum的简单使用

CBAM学习笔记
随机推荐
软件测试面试题:软件测试项目从什么时候开始?为什么?
首发展锐5G芯片!纯国产5G手机海信F50曝光:搭载虎贲T710+春藤510
@Component可以和@Bean 用在同一个类上吗?
LInkedList底层源码
30 minutes to thoroughly understand the synchronized lock upgrade process
自研5G芯片商用推迟?未来4年苹果iPhone都将采用高通5G芯片
ECCV 2022 | China University of science and Technology & jd.com proposed: data efficient transformer target detector
监听服务器jar运行,及重启脚本
Report design - how to make your powerbi Kanban brilliant?
软件测试面试题:设计测试用例时应该考虑哪些方面,即不同的测试用例针对那些方面进行测试?
三星最先进EUV产线已投产:今年7nm产能将是去年3倍
How to check whether there is Tsinghua source / delete Tsinghua source and keep the default source
排序(冒泡排序)后面学习持续更新其它排序方法
Can single mode and multi-mode of industrial switches replace each other?
Software testing interview question: what is the focus of unit testing, integration testing, and system testing?
Idea connects to MySQL database and performs SQL query operations
二维数组的基本用法
Characteristics and determination scheme of Worthington mushroom polyphenol oxidase
如何实现一个好的知识管理系统?
Software test interview question: when saving a text file under windows, a save dialog box will pop up. If a test case is established for the file name, how should equivalent classes be divided?