The object-oriented static,final keyword for Java learning notes

Source: Internet
Author: User

Java learning Note Object-oriented static,final keyword i. Static keyword

1. Overview:

Static, statically decorated members belong to a class, do not belong to a single object, are shared by all objects, exist in a static zone, and static members take precedence over objects loaded into memory.

How 2.STATC decorated members are used: (the static modified member variable has a default value)

  

/* 1. Can be used directly through the object, is not recommended to use 2. Call the static member class name by the class name. static member    variable    class name. Static Members Method * /

Features of 3.static

 

/* 1. In the same class, static members can only access static members, and non-static members have access to static members and non-static members. cause: Static members are loaded into memory first, and non-static members are loaded into memory only when the object is created. The 2.main method is a static member method and can be defined in any class simply as the execution portal of the program, not a class. */

4. Static code block:

Code block: Refers to the area of code that is wrapped by {}.

  

/* 1. Local code block: A    code block defined in a local location (inside a method), the main function is to change the scope of the variable; 2. Construct a code block: a    block of code that defines a member's location (outside of a method in a class), constructs a block of code that belongs to each object, and each time the object is created. will be executed once. Take precedence over the execution of    the construction method: "1" assigns the member variable "2" extracts the common code in the construction method, improves the code reusability 3. Static code block: a    block of code defined in the member's location (outside the method in the class), modified by static. Static code blocks belong to the class, not to the object, the lifetime is executed only once, the first execution (static precedence over the non-static, takes precedence over the object); function: "1" assigns the static variable "2" to the project initialization * /

5.static polymorphic call, compile and run;

Compile: Look at the parent class, the parent class has been compiled successfully, the parent class did not compile failed;

Run:

Member variables: All look at the parent class

Member method: Non-static run subclass rewrite

static method of running the parent class statically (not related to subclasses, polymorphism is the form of the parent class);

Cause: Static methods belong to classes, not objects, and static methods do not override this saying

The object's polymorphism, static and object-independent, references to the parent class. Static methods, which are called static methods of the parent class

Two. Final keyword

1. Understanding:

Final, cannot be changed;

2.final Features:

/* 1.  Final Modified class: The final Modified class cannot be inherited , the other is unchanged, can inherit other classes, also called Eunuchs class; 2.  When modifying a member method: The member method is a final method, cannot be overridden by a quilt class , and can be inherited using; 3.  When modifying a variable: The modified variable is a constant, where the value cannot be changed, the assignment is constant for a lifetime , and the local variable has only a final modifier; 4.  When modifying a member variable: is a constant whose value can only be assigned once and must be assigned before the object is created, and the modified member variable must display an assignment : "1" When the definition shows the assignment "2" uses the Construction code block "3" using the construction method * /
Three. Four permission modifiers

1.private: Private, homogeneous access only

2. Default: Is not write modifier, under the same package access

3.protected: protected; sub-class access under the same package or under different packages

4.public: public; under the same project

  

The object-oriented static,final keyword for Java learning notes

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.