Java Study Notes 14 (Object-Oriented 7: final and static), learning notes static

Source: Internet
Author: User

Java Study Notes 14 (Object-Oriented 7: final and static), learning notes static

Final: final, immutable, is a modifier.

Sometimes a class location function has been developed and does not want to rewrite or modify the subclass. The final keyword is used here.


Final modifier class:

It cannot be inherited, but it can inherit other classes.


Public final class Fu {public void show () {System. out. println ("method of the final class");} // This class cannot be inherited // The usage method remains unchanged}
public class Test {    public static void main(String[] args) {        Fu f = new Fu();;    }}


Final modification method:

The quilt class cannot be rewritten.


Public class Fu {public final void show () {System. out. println ("final method of parent class");} public void function () {System. out. println ("general method of parent class ");}}
Public class Zi extends Fu {public void function () {System. out. println ("subclass override general method of parent class");} // The show method of parent class cannot be rewritten}
Public class Test {public static void main (String [] args) {Zi z = new Zi (); // the usage of the method has not changed z. function (); z. show ();}}



The variable modified by final is called a constant and can only be assigned once:

One assignment, unchanged for life

If final modifies the reference data type, the memory address of the stored variable remains unchanged for life.


Final modifier member variables:

The member variables are stored in the heap memory and have default values. Therefore, the final modifier member variables must be assigned a value.

Since there are two methods to assign values to member variables: direct assignment and constructor assignment, you can assign values to final modified member variables.

However, make sure that the value can be assigned only once.


Public class Person {// assign values directly (this method is recommended in practice): // final int age = 18; // assign values to the constructor: final int age; public Person (int age) {this. age = age ;}}





A school has a group of students with different names and ages, but their school names are the same,

When a student object is created, the school name in the member variable is stored in heap memory every time the object is created, but the data stored each time is the same, resulting in a waste of memory.

So I thought about whether to put the school name in a certain place to share multiple objects and save memory.

Therefore, the static keyword is displayed:

There is one more static call method.

Static modified members can be called directly by class names.


public class Person {    String name;    static String className;}
Public class Test {public static void main (String [] args) {Person p1 = new Person (); Person p2 = new Person (); = "James "; = ""; p1.className = ""; // features of data sharing: modify one object, and change other objects to System. out. println (p2.className); System. out. println (Person. className) ;}// output: Class 1



Analyze static memory according to this example:

In the memory, static takes precedence over non-static

1. The main method in the Test. class file enters the data sharing zone (static zone ),

2. The className variable in the Person. class file enters the data sharing zone, and the default value is null.

3. Start execution and run the main method. Copy the main method from the JVM to the static zone and press the stack for execution.

4. Create an object and open a space storage object in the heap. The member variables follow and do not enter the static state because the static variables have already entered the static state zone.

5. From the JVM to the static zone, find the static attribute className of the Person class and perform modification and other operations.



Static considerations:

Static cannot call non-static:

Cause: claim period

The static state takes precedence over the non-static and inner state.

For example, the Ancients could not access modern people.

Non-static call of static

For example, modern people can access the ancients (it may be a bit inappropriate... understanding)

Static cannot use this, super method,

Similarly, the static object exists before it is created, and access to the object that does not exist is not allowed.

In practice, static and non-static


Public class Student {private String name; private static int age; public static void function () {// System. out. println (name); // The static method here cannot call non-static} public void show () {System. out. println (age); // non-static call static }}


Static modification application scenarios:

It is a member modifier. It can modify member variables and member methods.

Are there common data between multiple things? Here we can define this common data as static

As long as the method has not called a non-static member, it is defined as static


Static call in the object:

Here is a difficulty in polymorphism:


Public class Fu {static int I = 1; public static void show () {System. out. println ("static method of parent class ");}}
Public class Zi extends Fu {static int I = 2; public static void show () {System. out. println ("static method of subclass ");}}
Public class Test {public static void main (String [] args) {Fu f = new Zi (); System. out. println (f. i); f. show () ;}}/* output: 1. In the static method of the parent class * // * polymorphism, check the code on the left of the equals sign. The parent class has been compiled successfully, but the parent class does not, if the compilation fails, run the static method on the right. If it is a static method in the parent class, run the non-static method member variable rewritten by the subclass: regardless of static or non-static, compiling and running are the root cause of the parent class: static is a class, and non-input object polymorphism is the nature of the discussion object, but static is irrelevant to the object ,*/





In development, sometimes static constants need to be defined, and constant names must be in upper case, and spaces should be replaced _.

Fixed format: public static final String THIS_NAME = "XXXX"

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: 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.