Static additions in Java

Source: Internet
Author: User

Static:StaticA modified method, or class property, does not need to create an object instance of the class (New) can be used, directly with the class name. If your method or property does not change with the object instance, it can be modified with static, for example, for constants, in all cases, all of which we define as Public classNnmber { Public Static Final intNumbet_one = 1; }     Public: Indicates that all other classes can get the valueStatic: Represents Nnmber. This value can be obtained in numbet_one mode without the need for new Nnmber (). The way of NumbetFinal: The value that identifies the variable cannot be changed, the constant cannot change the value and the method of the JDK math class, the math is the same in any case; all the math classes are publicStaticOf If your method is unchanged in any case, you can define it as publicStaticThere is also a static method that can only invoke the static method, or static class properties, using public, or private, and static is not directly related PublicIndicates that all classes are public; pirvate means that they are not used in this class, which means that the same package is common=========================================Lei1: Public classClass 1 Name {intA = 10; StaticB = 20;} Lei2://Main Method//call B under static:     Public Static voidMain (string[] args) {System.out.println (lei1.b);//in a class that is not Lei1, if it is a static member variable that has been modified with static, you can use the class name directly. (member name) to call directly! //Print out the    }    ==========================================//do not call a under static:     Public Static voidMain (string[] args) {Lei1 DX=NewLei1 (); System.out.println (DX. A);//in the absence of static, A is not a static member variable, and the class name cannot be used directly.    (member name) to call directly, you need to create a DX object under the Lei1 class, and then access the a member variable on the DX object! //print out the ten!!!        Method is also true!!! }

Static additions in Java

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.