class

Static value initialization

With this example we are going to demonstrate how to make static values initialization in classes. In short, to make static values initialization in classes we have followed the steps below:

  • We have created a class A with a method function1(int i).
  • We have also created class B, that has two static A fields, b1 and b2. In its constructor it calls function1(int i) of A b2 field. It also has a method function2(int i).
  • Another class we have created is C, that has a field A b3, and two static fields A b4 and A b5. In its constructor it calls function1(int i) of A b4 field and it also has a method function3(int i).
  • In StaticInitial class, we have a static B t2 field that is initialized using B() constructor. First its two static fields are initialized and then in B() constructor function1() is called.
  • In StaticInitial class another static field is initialized, that is C t3. All static fields of C are initialized, and then the non static field is initialized and the constructor is called, that invokes function1() method of A.
  • A main() method in StaticInitial is called, where we create a new instance of C class. Its non static field is initialized and then its constructor is called, that calls function1().

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

class A {

    A(int i) {

  System.out.println("A(" + i + ")");
    }

    void function1(int i) {

  System.out.println("function1(" + i + ")");
    }
}


class B {

    static A b1 = new A(1);

    B() {

  System.out.println("B()");

  b2.function1(1);
    }

    void function2(int i) {

  System.out.println("function2(" + i + ")");
    }
    
    static A b2 = new A(2);
}


class C {

    A b3 = new A(3);
    static A b4 = new A(4);

    C() {

  System.out.println("Cupboard()");

  b4.function1(2);
    }

    void function3(int i) {

  System.out.println("function3(" + i + ")");
    }
    static A b5 = new A(5);
}

public class StaticInitial {

    public static void main(String[] args) {

  

  System.out.println("Creating new C() in main");

  new C();

  System.out.println("Creating new C() in main");

  new C();

  

  t2.function2(1);

  t3.function3(1);

    }
    static B t2 = new B();
    static C t3 = new C();
}

Output:

A(1)
A(2)
B()
function1(1)
A(4)
A(5)
A(3)
Cupboard()
function1(2)
Creating new C() in main
A(3)
Cupboard()
function1(2)
Creating new C() in main
A(3)
Cupboard()
function1(2)
function2(1)
function3(1)

  
This was an example of how to make static values initialization in classes in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button