Java: Getting started from scratch, learning notes basics & lt; Static & gt; (12)

Source: Internet
Author: User

The Static keyword indicates that it is static. It can be used to modify attributes and Methods. If an attribute in a class is modified using Static, this property is a static property. If the method in a class is modified using static, this method is a static method. If the internal class is modified using static, the internal class is a static internal class. If the attribute or method is modified by the static keyword, you can directly use the class name. static attribute name or class name. the Static method name "to call the Static keyword cannot modify the constructor package com. ibm. jbs. height; public class StaticDemo {// declare a static property // syntax format: access modifier + static + Type + attribute name = [value]; privatestatic inti; privatestatic Stringname = "admin"; public static Stringage; public Stringpass; // sound A static method // syntax format: access modifier + static + Return Value Type + method name (parameter list) {}publicstatic void show () {} publicstatic String show1 () {returnnull;} public String show2 () {returnnull;} // public static StaticDemo () {/// static cannot modify the static block of the constructor //}. static method, free block, normal method execution sequence first static block, then execute the main method, execute the free block when creating the object, and then execute the program package com. ibm. jbs. height; public class StaticDemo02 {public staticinti; publicstatic Stringname; static {I = 10; name = "admin "; System. out. println ("static block execution completed");} {System. out. println (I); System. out. println (name); System. out. println ("free block execution completed");} publicstatic void show () {System. out. println (I); System. out. println (name); System. out. println ("static method execution completed");} public void show2 () {System. out. println (I); System. out. println (name); System. out. println ("normal method execution completed");} publicstatic void main (String [] args) {show (); new StaticDemo02 (). s How2 () ;}} execution result: static block execution complete 10admin static method execution complete 10admin free block execution complete 10admin normal method execution complete Final keyword table execution can modify attributes, you can modify attributes by modifying the Final class. The attribute value does not change after the attribute initialization value. If the attribute is not initialized, you can only change the attribute value, objects that cannot change attributes. The Final modifier indicates that this method is the Final method, which cannot be overwritten. The Final modifier class indicates that this class is the final class. This class cannot be inherited. When the final modifier attribute is used, the attribute needs to be instantiated. When the Final modifier attribute is used, the attribute is a constant, constants are generally named in uppercase public class FinalDemo {// to uppercase: ctr1 + shift + x // to lowercase: ctrl + shif + y publicfinal StringNAME = ""; public static final StringPASS = ""; // attributes modified by final cannot be changed. publicstatic void main (String [] args) {// PASS = "AA ";}}. use final modifier: access modifier + final + Return Value Type + method name (parameter list) {} public final void show () {} the quilt class cannot overwrite the final modifier class using the final modifier method: access Modifier + fianl + class name {} Public final class FinalDemo {} the class modified with final cannot be inherited by other classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.