Java Learning (final, static keywords)

Source: Internet
Author: User
Tags modifier

Final keyword

Concept: final meaning is ultimately, immutable. Final is a modifier that can be used to modify a class , a member of a class , and a local variable . The construction method cannot be modified.

Characteristics:

A 1.final decorated class cannot be inherited , but can inherit another class.

class Yy {} Final class extends // can inherit yy class class extends // cannot inherit the FU class

2. The final decorated method can not be overridden by the quilt class , but the parent class is not in the final decorated method, and the subclass can be added final after rewriting.

class Fu {        //  Final modified method, cannot be overridden, but can inherit using public    finalvoid method1 () {}      Public void method2 () {}} class extends Fu {    // override Method2 method public    finalvoid method2 () {}}

3. final modified variables are called constants , which can only be assigned once. And life is the same .

Final int i =+// assignment error, final modified variable can only be assigned once

4. The value of the variable of the reference type is the object address value, the address value cannot be changed , but the object property value within the address can be modified .

Final New  new//final modified variable p, the recorded address value cannot be changed p.name = "Xiao Ming"; // You can change the value of the Name property in the P object

5. Modify the member variable , you need to assign a value before creating the object , otherwise error. (When there is no explicit assignment, you need to assign a value to multiple constructor methods.) )

class Demo {    // Direct Assignment    finalint m = +;         // final decorated member variables, which need to be assigned before the object is created, or an error.     finalint  n;       Public Demo () {        /// You can assign a value of n = to the variable n in the constructor that was called when the object was created        .    }

Static keyword

concept:static modifier is used to modify a member in a class. You can call a method without creating an object.

Characteristics:

1. the static modified member variable belongs to the class and does not belong to an object of this class. (That is, when multiple objects access or modify a static decorated member variable, one of the objects modifies the value of the static member variable, and the value of the static member variable in the other object changes, that is, multiple objects share the same static member variable )

classDemo { Public Static intnum = 100;}classTest { Public Static voidMain (string[] args) {Demo D1=NewDemo (); Demo D2=NewDemo (); D1.num= 200; System.out.println (D1.num); //The result isSystem.out.println (D2.num);//The result is    }}

2. members that are modified by static can and recommend direct access through the class name .

to access the format of a static member:

class name. Static member variable name Class name. Static member method name (parameter) object name. Static member variable name          ------ This method is not recommended, and a warning object name appears. Static member Method name (parameter     )------Not recommended for this way , a warning will appear

classDemo {//Static member variables     Public Static intnum = 100; //Static Methods     Public Static voidmethod () {System.out.println ("Static Method"); }}classTest { Public Static voidMain (string[] args) {System.out.println (demo.num);    Demo.method (); }}

Precautions:

1. static content takes precedence over the existence of an object, can only access static, and cannot use This/super. Statically decorated content is stored in a static area .

class Demo {    // member variable public    int num = +;     // Static Methods     Public Static void method () {        //this.num; cannot use This/super.         System.out.println (this. num);    }}

2. In the same class, a static member can only access static members

classDemo {//member Variables     Public intnum = 100; //Static member variables     Public Static intCount = 200; //Static Methods     Public Static voidmethod () {//System.out.println (num); In static methods, only static member variables or static member methods can be accessedSystem.out.println (count); }}

3. TheMain method is a static method that simply executes the entry for the program, which does not belong to any one object and can be defined in any class.

4. polymorphic call method , compile see = left , parent class has , compile succeeded , parent class not , compile failed

Run, static methods, run static methods in the parent class ,

Run, non-static method, overriding method for running subclasses

member variables, compile run all are parent class

Defining static Constants

Concept: In development, we want to define a static constant in the class, usually using the public static final modified variable to complete the definition. variable names are all capitalized , and multiple words are concatenated with underscores .

define the format: Public static final data type variable name = value;

Public class School {    publicstaticfinal String school_name = "Peking University"    ;  Public Static void method () {        System.out.println ("A static Method");}    }

When we want to use static members of a class, we do not need to create an object to access it directly using the class name .

System.out.println (company.school_name//  Call a static method

Attention:

each member variable in the interface uses the public static final decoration by default.

The member variable in all interfaces is already a static constant, and the assignment must be displayed because the interface does not have a constructor method . can be accessed directly with the interface name.

Interface Inter {    publicstaticfinalint COUNT = +;}

Member variables for the Access interface:

Inter.count

Java Learning (final, static keywords)

Related Article

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.