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;
        }
2
  • 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. :) Commented Mar 2, 2015 at 8: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. Commented Mar 2, 2015 at 8:46

6 Answers 6

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
    }

    // ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

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
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
    }
}

2 Comments

@TheLostMind I did not see the question has been edited?
@TheLostMind ok. Now what should i do?Can i update my answer?
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.

Comments

0

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

 boolean val=ABCServerUtil.checkServer();

some tutorial is given here

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.