Final and Static keywords, java

Source: Internet
Author: User

The final concept

The advent of inheritance increases the reusability of code and facilitates development. However, there are problems, some classes do not want to be inherited after the description, or some of the methods in the class function is fixed, do not want subclasses to rewrite. But when the subclass inherits these special classes, it can rewrite the method, how to solve it? To solve the above problems, you need to use a keyword final, finalmeaning is final, 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.

The final feature

  The final decorated class cannot be inherited, but other classes can be inherited .

  Final In addition to the construction method can not be modified, the rest can be modified ;

class Yy {}

Final class Fu extends yy{}//can inherit Yy class

class Zi extends fu{}//cannot inherit Fu class

  Final Retouching Method It is not possible to override the quilt class but can inherit, the parent class is not in the final decorated method, and the subclass can be added to final when overridden.

class Fu {

The final modified method cannot be overridden, but can be inherited using the

Public Final void method1 () {}

Public void method2 () {}

}

class Zi extends Fu {

Overriding the Method2 method

Public Final void method2 () {}

}

  final-modified variables are called Constants , and they can only be assigned once. And life is the same .

Final int i = 20;

  I = 30; Assignment error, final modified variable can only be assigned once

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

Final Person p = new person ();

person P2 = new person ();

  p = p2; Final modified variable p, the recorded address value cannot be changed

P.name = "xiaoming";//You can change the value of the Name property in the P object

P cannot be a different object, and the value of the name or age property in the P object can be changed.

  Modify the member variable, you need to assign a value before creating the object, otherwise error. You can assign a value by constructing it (when there is no explicit assignment, you need to assign a value to multiple constructor methods

class Demo {

//Direct Assignment

Final int m = 100;

//final modified member variable, you need to assign a value before creating the object, otherwise error.

Final int N;

Public Demo () {

//You can assign a value to the variable N in the constructor method that is called when the object is created

n = 2016;

}

}

Static concept

When you define a class, you have the appropriate properties and methods in the class. The properties and methods are called by creating this class of objects. When a method of an object is called, the method does not have access to the object's unique data, and the method creates the object somewhat superfluous. But do not create the object, the method can not be called, then you will think, then we could not create the object, we could call the method?

Yes, we can do that by using the static keyword. static modifier, which is typically used to decorate a member of a class.

Static features

  a member variable that is modified by static belongs to a 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)

Code Demo:

class Demo {

Public Static int num = 100;

}

class Test {

Public Static void Main (string[] args) {

Demo D1 = New demo ();

Demo D2 = New demo ();

D1. num = 200;

System. out. println (D1. Num); Result is 200

System. out. println (D2. Num); Result is 200

}

}

  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)

  The name of the object. Static member variable name------It is not recommended to use this method, a warning appears

  The name of the object. Static member Method name (parameter)------This method is not recommended, a warning appears

Code Demo:

class Demo {

Static member variables

Public Static int num = 100;

Static methods

Public Static void method () {

System. out. println ("static method");

}

}

class Test {

Public Static void Main (string[] args) {

System. out. println (Demo. Num);

Demo. Method ();

}

}

Static considerations

  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 variables

Public int num = 100;

Static methods

Public Static void method () {

This.num; You cannot use This/super.

System. out. println (this. num);

}

}

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

class Demo {

Member variables

Public int num = 100;

Static member variables

Public Static int count = 200;

Static methods

Public Static void method () {

SYSTEM.OUT.PRINTLN (num); In a static method, only static member variables or static member methods can be accessed

System. out. println (count);

}

}

The main 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.

L 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, compiling run It's all the parent class .

Defining static constants

In development, we want to define a static constant in the class, usually using a variable that is modified by the public static FINAL to complete the definition. At this point the variable name is in all uppercase , and multiple words are concatenated with underscores .

Define the format:

public static final data type variable name = value;

The following shows:

class School {

Public Static Final 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);

Company. Method (); 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 {

public static final int COUNT = 100;

}

Static variables in the Access interface

Inter.count

Final and Static keywords, java

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.