当前位置:网站首页>2021-11-05理解main方法语法、代码块及final关键字
2021-11-05理解main方法语法、代码块及final关键字
2022-07-27 19:07:00 【小唐学抓娃】
= = = = = = = 一、理解main方法语法 = = = = = = = =
一、解释main方法的形式:public static void main(String[] args)
1. main方法时虚拟机调用
2. java虚拟机需要调用类的main()方法,所以该方法的访问权限必须是public
3. java虚拟机在执行main)方法时不必创建对象,所以该方法必须是static
4.该方法接收String类型的数组参数,该数组中保存执行java命令时传递给所运行的类的参数
5. java 执行的程序参数1参数2参数3
二、特别提示
1) 在 main()方法中,我们可以直接调用 main 方法所在类的静态方法或静态属性。
2) 但是,不能直接访问该类中的非静态成员,必须创建该类的一个实例对象后,才能通过这个对象去访问类中的非静态成员
= = = = = = = 二、理解代码块 = = = = = = = =
一、基本介绍:
代码化块又称为初始化块,属于类中的成员[即是类的一部分,类似于方法,将逻辑语句封装在方法体中,通过{}包围起来。
但和方法不同,没有方法名,没有返回,没有参数,只有方法体,而且不用通过对象或类显式调用,而是加载类时,或创建对象时隐式调用。
二、基本语法:
[修饰符]{
代码
};
说明注意:
1)修饰符可选,要写的话,也只能写static
2)代码块分为两类,使用static修饰的叫静态代码块,没有static修饰的,叫普通代码块/非静态代码块.
3)逻辑语句可以为任何逻辑语句(输入、输出、方法调用、循环、判断等)
4);号可以写上,也可以省略。
三、基本理解:
1)相当于另外一种形式的构造器(对构造器的补充机制),可以做初始化的操作
2)场景:如果多个构造器中都有重复的语句,可以抽取到初始化块中,提高代码的重用性。
四、注意事项与细节:
1) static代码块也叫静态代码块,作用就是对类进行初始化,而且它随着类的加载而执行,并且只会执行一次。如果是普通代码块,每创建一个对象,就执行。
2)类什么时候被加载[重要背!]
①创建对象实例时(new)
②创建子类对象实例,父类也会被加载
③使用类的静态成员时(静态属性,静态方法)
3)普通的代码块,在创建对象实例时,会被隐式的调用。
被创建一次,就会调用一次。
如果只是使用类的静态成员时,普通代码块并不会执行。
小结:1.static代码块是类加载时执行,只会执行—次
2.普通代码块是在创建对象时调用的,创建一次,调用一次
3.类加载的3种情况,需要记住.
= = = = = = = 三、final关键字 = = = = = = = =
一、基本介绍:
final可以修饰类、属性、方法和局部变量.
在某些情况下,程序员可能有以下需求,就会使用到final:
1)当不希望类被继承时,可以用final修饰.
2)当不希望支类的某个方法被子类覆盖/重写(override)时,可以用final关键字修饰。
3当不希望类的的某个属性的值被修改,可以用final修饰.
4)当不希望某个局部变量被修改,可以使用final修饰
二、注意事项和使用细节:
1)final修饰的属性又叫常量,一般用XX_XX_ XX来命名
2) final修饰的属性在定义时,必须赋初值,并且以后不能再修改,赋值可以在如下位置之一【选择一个位置赋初值即可】:
①定义时:如public final double TAX_RATE=0.08;
②在构造器中
③在代码块中。
3)如果final修饰的属性是静态的,则初始化的位置只能是:
①定义时②在静态代码块
不能在构造器中赋值。
4) final类不能继承,但是可以实例化对象。
5)如果类不是final类,但是含有final方法,则该方法虽然不能重写,但是可以被继承。
边栏推荐
- LInkedList底层源码
- 腾讯云[HiFlow】| 自动化 -------HiFlow:还在复制粘贴?
- MobileVIT学习笔记
- Comprehensively design an oppe home page -- the style of the search and oper part of the page
- Oppo core making plan officially announced: the first chip or oppo M1
- Mobilevit learning notes
- 一篇文章带你走进pycharm的世界----别再问我pycharm的安装和环境配置了!!!
- Can China make a breakthrough in the future development of the meta universe and occupy the highland?
- ACM mm 2022 | Zhejiang University proposed: point cloud segmentation, active learning of new SOTA
- C language - Introduction - grammar - pointer (12)
猜你喜欢

MobileVIT学习笔记

Guava Cache 原理分析与最佳实践

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

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

C语言-入门-语法-指针(十二)

JVM-内存模型 面试总结

C language - Introduction - grammar - pointer (12)

Multi person collaborative development specification

Why use MQ message oriented middleware? These questions must be solved

Can single mode and multi-mode of industrial switches replace each other?
随机推荐
技术管理 - 一定要抓大放小
puzzle(002)内固、外固、哈密顿
中英文说明书丨 AbFluor 488 细胞凋亡检测试剂盒
Characteristics and determination scheme of Worthington mushroom polyphenol oxidase
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
Worthington蘑菇多酚氧化酶的特性及测定方案
Can China make a breakthrough in the future development of the meta universe and occupy the highland?
Search, insert and delete of hash table
Zibbix installation and deployment
最高7.5Gbps!全球首款5nm 5G基带骁龙X60发布:支持聚合全部主要频段!
[what subjects does Huawei hcie security test? What knowledge points does Huawei hcie security test have?]
内部类(四种内部类详解)
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
Chinese and English instructions - human alpha fetoprotein (AFP) ELISA quantitative Kit
Acwing3715. Minimum exchange times (simulation idea of bubble sorting method)
Oppo core making plan officially announced: the first chip or oppo M1
中英文说明书丨人甲胎蛋白(AFP)ELISA定量试剂盒
聊聊 MySQL 事务二阶段提交
Will the United States prohibit all Chinese enterprises from purchasing American chips? Trump responded like this