class
Static fields inheritance
This is an example of static fields inheritance between two classes. The steps of the example are described below:
- We have created a class
Aclass
that has astatic
int val, and a static methodfun()
that returns an int value. - We have also created a subclass of
Aclass
, that isInheritanceStaticFields
and extends the Aclass. - We create a new instance of
InheritanceStaticFields
and call itsstart()
method, that prints the results of the values inherited to this class byAclass
.
Let’s take a look at the code snippet that follows:
package com.javacodegeeks.snippets.core; class Aclass { static int val = 24; Aclass() { System.out.println("In Aclass::<init>"); } static int fun() { return 12; } } /** * This is the subclass */ public class InheritStaticFields extends Aclass { public static void main(String[] c) { new InheritStaticFields().start(); } public void start() { System.out.println("In InheritStaticFields::start"); System.out.println("Val is " + val); System.out.println("fun is " + fun()); } }
Output:
In Aclass::<init>
In InheritStaticFields::start
Val is 24
fun is 12
This was an example of static fields inheritance between two classes in Java.