当前位置:网站首页>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 .
边栏推荐
- ECCV 2022 | China University of science and Technology & jd.com proposed: data efficient transformer target detector
- 枚举和注解
- Samsung's most advanced EUV production line has been put into operation: the 7Nm capacity this year will be three times that of last year
- 成员方法及其传参机制
- Mobilevit learning notes
- 深入理解递归的方法调用(含实例迷宫问题、汉诺塔、猴子吃桃、斐波拉契、阶乘))
- University of Tilburg, Federal University of the Netherlands | neural data to text generation based on small datasets: comparing the added value of two semi supervised learning approvals on top of a l
- CBAM learning notes
- zibbix安装部署
- 枚举Enum的简单使用
猜你喜欢

枚举Enum的简单使用

LInkedList底层源码

【2022牛客多校第二场】K-Link with Bracket Sequence I

Puzzle (021) eliminate problems

IDEA常用快捷键及设置方法

Mobilevit learning notes

一篇文章带你走进pycharm的世界----别再问我pycharm的安装和环境配置了!!!

Can China make a breakthrough in the future development of the meta universe and occupy the highland?

zibbix安装部署

C语言-入门-语法-指针(十二)
随机推荐
软件测试面试题:软件验收测试包括正式验收测试、alpha测试、beta测试三种测试?
Openai issued a document to introduce the latest application of Dall · E 2: fully enter the field of artistic creation and design
2019Q4内存厂商营收排名:三星下滑5%,仅SK海力士、美光维持增长
Software test interview question: does software acceptance test include formal acceptance test, alpha test and beta test?
Zibbix installation and deployment
ADB ~ hide or disable the status bar and virtual keys
30 minutes to thoroughly understand the synchronized lock upgrade process
为什么要使用MQ消息中间件?这几个问题必须拿下
Simple manual implementation of map
高并发遇到死锁了,如何搞?
PostgreSQL source code (65) analysis of the working principle of globalvis, a new snapshot system
Acwing3715. Minimum exchange times (simulation idea of bubble sorting method)
首发展锐5G芯片!纯国产5G手机海信F50曝光:搭载虎贲T710+春藤510
@Autowired注解与@Resource注解的区别
ACM mm 2022 | Zhejiang University proposed: point cloud segmentation, active learning of new SOTA
QT take out the input box string, lineedit
Software test interview question: please say who is the best person to complete these tests, and what is the test?
Principle analysis and best practice of guava cache
@RequestParam注解的详细介绍
DAY_ 4. Operation -- judge whether there is a certain data in the array -- realize array mapping (enlarge by 10 times) -- insert the array in sequence (modify bugs) -- realize array de duplication