Java language static variables and static method inheritance problems

Source: Internet
Author: User

first, define a Class A as follows

  1. Class A {
  2. static int a = 1;
  3. static int b = 2;
  4. public static void PrintA () {
  5. System.out.println (a);
  6. }
  7. public static void Printb () {
  8. System.out.println (b);
  9. }
  10. }
Class A {static int a = 1;    static int b = 2;    public static void PrintA () {System.out.println (a);    } public static void Printb () {System.out.println (b); }}

A has a static variable, a, B, and there are two static methods Printa () and PRINTB (), which are used to print the value of A, b

Redefine a Class B inheritance, A and B, and the static Method Printb () .

  1. Class B extends A {
  2. static int a = 3;
  3. static int b = 4;
  4. public static void Printb () {
  5. //If B is not redefined in B, it is called a.b, and after redefinition, the call is b.b
  6. System.out.println (b);
  7. }
  8. }
Class B extends a {static int a = 3;    static int b = 4;    public static void Printb () {//if B is not redefined in B, it will be called a.b, after redefinition, is called b.b System.out.println (b); }}


What if the call to B.printa () will print? What does the call B.PRINTB () print? Write a program to test it, the results are as follows:

  1. Public class Testoverridestatic {
  2. public static void Main (string[] args) {
  3. //b inherit from a static method, note: b Although the value of a is also defined (B.A), but because the call Printa is a method in a, he is printing a static variable A (A.A) value
  4. B.printa (); //[1]
  5. //If PRINTB () is not defined in B, it will be called PRINTB () in a, but because B is also defined in B's printb (), it is called B's own method, so
  6. //Print is also the value of static variable B (b.b) in B
  7. B.PRINTB (); //[2]
  8. //b can also call a directly, if there is no redefinition in B, call the A variable that inherits from a, redefine it, and call its own a
  9. System.out.println (B.A);
  10. }
  11. }
public class Testoverridestatic {public static void main (string[] args) {///b inherits static methods from A, note: b Although the value of a is also defined (B.A), But since the call to PrintA is a method in a, he prints the value of the static variable A (A.A) in A, B.printa (); [1]//if PRINTB () is not defined in B, it is called PRINTB () in a, but because B is also defined in B's printb (), it is called B's own method, so//printed is also the value of static variable B (b.b) in B b.printb (); [2]//B can also call a directly, if there is no redefinition in B, call the A variable inherited from a, redefine it, call its own aSystem.out.println (B.A);}}

Printing results:

1
4
3

Summarize:

1. Static variables and static methods say inheritance is not accurate, static methods and variables are the methods and variables belonging to the class. Subclasses also belong to the superclass, such as manage extends employee, then manage is also an employee, so subclasses can call static variables and methods that belong to the superclass. Notice that the subclass calls the static methods and variables of the superclass rather than the static methods and variables that inherit from the Super class. But if there are static methods and variables with the same name in the subclass, then the subclass itself is called, because the static and static methods of the subclass hide the static methods and variables of the parent class.

2. If a variable and method with the same name is not defined in the subclass, call the subclass name. static method/variable called the parent class method and variable

3,. If the subclass has only a static variable with the same name defined, and no static method with the same name as the parent class is called, the "subclass name. Static method" is called when the static method of the parent class is invoked, and the static variable in the static method is also the parent class (such as note [1] in the program)

4. If a child class defines a static variable with the same name as the parent class and a static method with the same name as the parent class, the call to the "subclass name. Static method" is completely unrelated to the parent class, and the static variable inside is also a subclass (such as note [2] in the program)

Java language static variables and static method inheritance problems (goto)

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.