Java Basics (i)

Source: Internet
Author: User

Java keywordsFinalDataBasic data type: Keep values constant
Reference data type: References do not change, that is, you cannot reference other objects. But the referenced object can be modified
MethodDeclaring a method cannot be overridden by a quilt class.
The private method is implicitly specified as final, and if the method defined in the subclass and a private method signature in the base class are identical, the method of the subclass is not overriding the base class method, but a new method is defined in the subclass.
classClass cannot be inherited
Static

1. Static variables

Static variables only have one copy in memory and are assigned only once when the class is initialized.

    • Static variables: All instances of the class share static variables, which can be accessed directly through the class name;
    • Instance variables: Each time an instance is created, an instance variable is generated, which is die with the instance.
A {    int x;        //Static variable} 

2. Static methods

A static method exists when the class is loaded, and it does not depend on any instance, so the static method must be implemented, that is, it cannot be an abstract method.

3. Static statement block

A static statement block runs once when the class is initialized.

4. Static Inner class

One of the inner classes, the static inner class does not depend on the outer class, and cannot access the non-static variables and methods of the outer class.

5. Static Guide Package

import static com.xxx.ClassName.*

The use of static variables and methods without the need to indicate ClassName, which simplifies the code, but greatly reduces readability.

6. Variable Assignment order

The assignment of static variables and the execution of static statement blocks takes precedence over the assignment of an instance variable and the operation of a normal statement block, and the assignment of static variables and the run of static statement blocks depends on their order in the code.

"Static variable ";
static {    System.println ("static statement block ");}    
"Instance variable ";
{    System.println ("normal statement block ");}    

Last Run constructor

Public Initialordertest () {    System.println ("constructor ");}    

In the case of inheritance, the order of initialization is:

    1. Parent class (static variable, static statement block)
    2. Subclass (static variable, static statement block)
    3. Parent class (instance variable, normal statement block)
    4. Parent class (constructor)
    5. Subclass (instance variable, normal statement block)
    6. Sub-class (constructor)
Content from https://github.com/CyC2018/Interview-Notebook/blob/master/notes/Java/

Java Basics (i)

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.