"Java Summary" static, static code block

Source: Internet
Author: User

Static

Static can be decorated with properties, methods, code blocks, inner classes

Characteristics
    1. Load as the class loads
    2. Precedence over the existence of objects
    3. Decorated members are shared by all objects
    4. When access permission is allowed, the object can not be created and called directly by the class
      Static modified properties, which are stored in memory, are only one member of the entire program.
 Public classTest { Public Static void Main(string[] args) {Person p =NewPerson (); Person.setname ("TTF");//class can access static methods directlySystem. out. println (Person.name);//Use class to access static properties directly}}class Person {StaticString name;//static properties,    intAge//member properties, which belong to the object's     Public Static void SetName(String name)    {person.name = name; }}
Use
    • If a property in a class is decorated with static, this property can only be accessed directly by the class (not by using private adornments), or by a class method with static adornments
    • The instance object of the class cannot access static properties (properties with static adornments), and static methods (methods with static adornments)p.setName("ttf")//错误,p不能访问静态方法同时也不能访问静态属性。
    • Static methods cannot access member variables of a class (properties that do not have static adornments), and static methods are class methods
    • Once the static property is initialized, which is initialized only once, the entire class has only one variable
Static code block
    1. A static code block is a code fragment that uses the static modifier.
    2. If there is more than one static block of code, proceed in order from top to bottom
    3. Static code blocks are executed before non-static blocks of code
    4. Static code block executes only once
    5. Classes are initialized when they are used.
publicclass Test {    publicstaticvoidmain(String[] args) {        //Person p = new Person();        System.out.println(Person.name);//ttf    }}class Person {    static String name;    int age;    /*静态代码块*/       static {        "ttf";    }}

"Java Summary" static, static code block

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.