Java Learning (vii)

Source: Internet
Author: User

Static members
    • A static member of a class is often referred to as a class member because a static member is associated with a class, not an instance of the class. Non-static member variable methods are often referred to as instance members because non-static member variables and methods exist only in instances of the class
    • Static member variables and methods cannot be accessed using references because references refer to instances of the class and do not require instances of the class to access static members. To access a static member to use the class name
    • Static methods cannot access instance members
Static initialization blocks

A static initialization block is a set of statements that are executed when a class is loaded by the class loader of the JVM. The static initialization block acts by allowing the class to perform any required setup tasks that need to occur only once

        static{        语句出现在这里 主体}
Instantiating an initialization block

Like a static initialization block, it is executed once for each instantiation of the object in the class. Instantiating an initialization block before the constructor call executes

    public class Test{        public Test(){构造函数}        {实例初始化块}        }
    • Execution order: the corresponding constructor in the subclass is called, the call to super is executed, the control flow jumps to the corresponding parent class constructor, the parent class constructor jumps back to the subclass constructor, and before the statement executed by the super in the subclass constructor, the instantiation initialization block executes
Inner class

Internal classes can be divided into four types:

    • Static inner class: As a static member of a class, the inside of a class exists
    • member Inner class: As a member of a class, exists inside a class
    • Local inner class: A class that exists inside a method
    • Anonymous inner class: A class that exists inside a class but has no class name

      public class Test{    static class Inner{静态内部类}}

Static inner class You can directly create an object of a static inner class without creating an object of the outer class, you can also reference the static member variable and the static method of the external class, but you cannot reference the ordinary members of the outer class

    public class Test{        class Outer{        class Inner{成员内部内}        }    }

A member inner class can invoke all members of an external class. However, you can only refer to external members after you have created an object of the outer class.

    public class Test{        public void f(){        class Inner{成员内部内}            }        }    }

In a local inner class, to use a local variable in the method it is in, you need to define the local variable as final

    new类或者接口的名字(){        匿名内部类的主体            }

The anonymous inner class is a special inner class that does not have a name. The definition of an anonymous inner class is merged with the creation of the object.
An anonymous inner class is a class that is defined for a unique object, and when we only need to create an object of a class and cannot use its name, an anonymous inner class can be used

Java Learning (vii)

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.