sharadendu sinha
sharadendu sinha

Reputation: 827

Nesting calls to static methods

I want to do something like below, but it does not work. My objective is to be able to nest function calls to static helper classes to get more brevity.

public class StaticHelper {
    public static Class<StaticHelper> doSomthing() {
        System.out.println("I just did something !!");
        return StaticHelper.class;
    }

    public static Class<StaticHelper> doSomthingElse() {
        System.out.println("I just did something else !!");
        return StaticHelper.class;
    }

    public static void main(String[] args) {
        // Does not compiles 
        StaticHelper.doSomthing().doSomthingElse();
    }
}

Is this possible? If so a simple example as above will be very helpful.

Upvotes: 1

Views: 211

Answers (2)

Adam Michalik
Adam Michalik

Reputation: 9945

That is not possible, but using static imports is.

public class StaticHelper {
    public static void doSomething() {
        System.out.println("I just did something !!");
    }

    public static void doSomethingElse() {
        System.out.println("I just did something else !!");
    }
}

in another class:

import static StaticHelper.*;

class Other {
    public static void main(String[] args) {
        doSomething(); // calls static methods from StaticHelper
        doSomethingElse();
    }
}

or - if the methods are logically connected - you can have one static factory method and the rest are instance methods:

public class StaticHelper {
    public static void beginDoingSomething() {
        // static factory method - you can pass parameters to it if needed
        System.out.println("I just did something !!");
        return new StaticHelper();
        // if needed, initialize the instance with the parameters
    }

    public StaticHelper andDoSomethingElse() {
        // instance method
        // can use the instance parameters (passed to the constructor in the static factory method)
        // or use parameters passed to this method 
        System.out.println("I just did something else !!");
        return this;
        // returns this for chaining
    }
}

in another class:

import static StaticHelper.*;

class Other {
    public static void main(String[] args) {
        doSomething().andDoSomethingElse().andDoSomethingElse();
    }
}

If you name the methods nicely, you can form a sentence:

validate(object).checkEmail().checkName().checkTelephoneStartsWith("+11");

where validate(object) is a static factory method constructing a new validator instance for the given object.

Upvotes: 2

Mateusz Korwel
Mateusz Korwel

Reputation: 1148

I guess you want something like this.

public class StaticHelper {
    private final static StaticHelper INSTANCE = new StaticHelper();

    public static StaticHelper doSomthing(){
        System.out.println("I just did something !!");
        return INSTANCE;
    }

    public static StaticHelper doSomthingElse(){
        System.out.println("I just did something else !!");
        return INSTANCE;
    }

    public static void main(String[] args) {
        StaticHelper.doSomthing().doSomthingElse();
    }
}

or another way

public class StaticHelper {
    public static SomeClass doSomthing(){
        return new SomeClass().doSomthing();
    }

    public static SomeClass doSomthingElse(){
        return new SomeClass().doSomthingElse();
    }

    public static void main(String[] args) {
        StaticHelper.doSomthing().doSomthingElse();
    }

    private static class SomeClass {

        public SomeClass doSomthing(){
            System.out.println("I just did something !!");
            return this;
        }

        public SomeClass doSomthingElse(){
            System.out.println("I just did something else !!");
            return this;
        }
    }
}

Upvotes: 2

Related Questions