static修饰符- Core Java 学习笔记

本文详细介绍了Java中静态成员的概念及应用,包括静态变量、静态方法、静态初始化块等,解释了它们的特点、初始化时机以及与普通成员的区别。此外,还讨论了静态方法的覆盖问题及其与多态的关系。
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <meta content="Word.Document" name="ProgId"> <meta content="Microsoft Word 11" name="Generator"> <meta content="Microsoft Word 11" name="Originator"> <link href="file:///C:%5CDOCUME~1%5CADMINI~1%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml" rel="File-List"> <!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning /> <w:DrawingGridVerticalSpacing>7.8 pt</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL /> <w:BalanceSingleByteDoubleByteWidth /> <w:DoNotLeaveBackslashAlone /> <w:ULTrailSpace /> <w:DoNotExpandShiftReturn /> <w:AdjustLineHeightInTable /> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> <w:UseFELayout /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><style type="text/css"> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"\@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:1069233763; mso-list-type:hybrid; mso-list-template-ids:1849608586 -2136555902 67698713 -1397579738 1786703264 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-text:%1.; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-36.0pt;} @list l0:level2 {mso-level-number-format:alpha-lower; mso-level-text:"%2\)"; mso-level-tab-stop:42.0pt; mso-level-number-position:left; margin-left:42.0pt; text-indent:-21.0pt;} @list l0:level3 {mso-level-text:(%3); mso-level-tab-stop:78.0pt; mso-level-number-position:left; margin-left:78.0pt; text-indent:-36.0pt;} @list l0:level4 {mso-level-start-at:4; mso-level-number-format:japanese-counting; mso-level-text:%4、; mso-level-tab-stop:99.0pt; mso-level-number-position:left; margin-left:99.0pt; text-indent:-36.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

static修饰符

<!--[if !supportLists]-->1. <!--[endif]-->可以修饰属性、方法、初始代码块,成为类变量、静态方法、静态初始化代码块。

注:初始代码块是在类中而不再任何方法之内的代码块。

<!--[if !supportLists]-->2. <!--[endif]-->类变量、静态方法、静态初始化代码块与具体的某个对象无关,只与类相关,是全类公有的。在类加载时初始化。

<!--[if !supportLists]-->3. <!--[endif]-->类加载:虚拟机通过CLASSPATH从磁盘上找到字节码文件,并将字节码文件中的内容通过I/O流读到虚拟机并保存的过程。在虚拟机的生命周期中一个类只被加载一次。

注:Java命令的作用是启动JVM

<!--[if !supportLists]-->4. <!--[endif]-->Static定义的时一块为整个类共有的一块存储区域,其发生变化时访问到的数据都是经过变化的。

<!--[if !supportLists]-->5. <!--[endif]-->为什么主方法必须是静态的?

主方法是整个应用程序的入口,JVM只能通过类名去调用主方法。

<!--[if !supportLists]-->6. <!--[endif]-->类变量和静态方法可以在没有对象的情况下用:类名.方法名(或属性名)来访问。

<!--[if !supportLists]-->7. <!--[endif]-->静态方法不可被覆盖(允许在子类中定义同名的静态方法,但是没有多态);父类如果是静态方法,子类不能覆盖为非静态方法。父类如果是非静态方法,子类不能覆盖为静态方法。

争论:静态方法可以覆盖但是没有多态。

思考:没有多态的覆盖叫覆盖吗?

在静态方法中不允许调用本类中的非静态成员。

<!--[if !supportLists]-->8. <!--[endif]-->静态初始化代码块只在类加载的时候运行一次,以再也不执行了。所以静态代码快一般被用来初始化静态成员。

<!--[if !supportLists]-->9. <!--[endif]-->不加static为动态初始化代码块,在创建对象时被调用(在构造函数之前)。

<!--[if !supportLists]-->10. <!--[endif]-->最后要注意的一点就是Static不能修饰局部变量。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值