0

I have a class similar to the below one with all static methods. Hence the class was not initialized while used in other classes. I have to check a condition before invoking any static methods from this class. Even if i add a default constructor it will not get called. Could someone suggest ideas to have solution without instantiating this class in all of its usages? It need be a default constructor could be a simple other solution.

I need to check everytime the network connectivity before making the call. Static Initializer gets called only first time on load.

        public class ABCServerUtil {

        public static boolean checkServer() {...bla...bla...}


        }

I need some thing like below piece of code to be called and to be exit.

        public ABCServerUtil(){
        if(!isNetworkOnline())
        return;
        }
FindIt
  • 807
  • 6
  • 14
  • 2
    **Hence the class was not initialized while used in other classes.** - How do you know that it didn't get *initialized*? You don't have any static initializer blocks.. And please explain in detail what is happening and what isn't. :) – TheLostMind Mar 02 '15 at 08:44
  • What you are describing is not an object and thus needs no constructor. You are defining static behavior and thus need to program it as such. – Jared Mar 02 '15 at 08:46

6 Answers6

2

If you need to check the condition every time one of the static methods is called, you don't have much choice but to do what you're doing: Call a method to do the check at the beginning of each of those methods.

If you only need to check the condition once when the class is initially loaded/initialized, you can use a static initializer:

public class ABCServerUtil {

    static {
        // Code here runs when the class is loaded
    }

    // ...
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • *If you need to check the condition every time one of the static methods is called,* - He could use a Proxy to call all his methods here :P – TheLostMind Mar 02 '15 at 08:54
1

Use a static Initialization block

static {
    //whatever code for initialization
}
  • A class can have any number of static initialization blocks
  • they can appear anywhere in the class body
  • static initialization blocks are called in the order that they appear in the source code.

You should be called every time when method called

public class Test {

    public static void checkServer() {
        if (!checkNetwork()) {
            return;
        }
    }

    public static void checkClient() {
        if (!checkNetwork()) {
            return;
        }
    }

    private static boolean checkNetwork() {
        return true; // or false depending on network condition
    }
}
Arun Kumar
  • 2,874
  • 1
  • 18
  • 15
0

You can use a static initialiser.

static {
    // code here
}

It will be run before any method of property (static or otherwise) of the class is first accessed.

Sam
  • 8,330
  • 2
  • 26
  • 51
0

you can directly call a static method with the class name like this,

 boolean val=ABCServerUtil.checkServer();

some tutorial is given here

Balayesu Chilakalapudi
  • 1,386
  • 3
  • 19
  • 43
0

Since there's already 5 answers saying the same thing and none of them seem to be what you're after:

A tool like Byte Buddy sounds like what you need.

Henry
  • 337
  • 1
  • 5
  • 12
-1

I think that this is your solution: Static initializer in Java

In practice you need a block of code executed the first time that your class is loaded.

Community
  • 1
  • 1
Giovanni
  • 203
  • 1
  • 8