简体   繁体   中英

Want to call a non-static Interface method in a static method. How?

I facing a real hard problem in my code snippet.

I want to learn how to use Interface in Java the correct way.

So for this I have my Application-Class...

package inversionUsage;

public class Application {

    public static void main(String [] args) {
        String standard = "Standard version!";

        if (FeatureDecisions.checkEnabledFeatures("new-feature1")) {
            System.out.println("Implement new feature...");
        }else {
            System.out.println(standard);
        }

    }

}

Then I made a Interface...

package inversionUsage;

public interface AppConfiguration {

    boolean isEnabled(String searchFeature);

}

I want to use the Interface in another class:

package inversionUsage;

import java.util.Arrays;

public class FeatureDecisions implements AppConfiguration{

    public String [] enabledFeatures;
    public String [] _implNewFeature = fetchFeatureTogglesFromSomehere();

    public static boolean checkEnabledFeatures(String searchFeature) {
        return isEnabled(searchFeature);
    }

    @Override
    public boolean isEnabled(String searchFeature) {

        if (Arrays.asList(_implNewFeature).contains(searchFeature)) {
            return true;
        }else {
            return false;
        }

    }

    private String [] fetchFeatureTogglesFromSomehere() {
        // TODO get the CONFIG from somewhere
        enabledFeatures = new String [2];
        enabledFeatures[0] = "new-feature1";
        enabledFeatures[1] = "new-feature2";
        return enabledFeatures;
    }

}

So the workflow is: 1. I start the Application 2. Main method checks the enabled features via FeatureDecisions.java 3. In Feature Decisions i implemented the Interface

I getting the error:

Cannot make a static reference to the non-static method isEnabled(String) from the type FeatureDecisions

May Someone can help me out?

The only way to use an instance method is to have an instance on which to call it. Your checkEnabledFeatures is static, so it doesn't receive an instance you can use (as this ). To use an instance method, it would need to create an instance. But obviously that's not what you want here.

Java's interface construct is for defining the interface that instances implement. Java doesn't have the concept of a "static interface" that a class must implement. On the rare occasions when that's needed, it's usually implemented using reflection (perhaps with a class-level annotation to indicate that the class has the necessary feature).

You would have to instantiate the FeatureDecisions class.

public static boolean checkEnabledFeatures(String searchFeature) {
    return new FeatureDecisions().isEnabled(searchFeature);
}

or make all members static.

Additional info: There are frameworks like togglz that do this for you.

There's no way to do that. The closest can get is to use the singleton pattern (though lots of people - myself included - would discourage it).

public enum FeatureDecisions implements AppConfiguration
{
    INSTANCE;

    public String [] enabledFeatures;
    public String [] _implNewFeature = fetchFeatureTogglesFromSomehere();

    public boolean checkEnabledFeatures(String searchFeature) {
        return isEnabled(searchFeature);
    }

    @Override
    public boolean isEnabled(String searchFeature) {
        //...
    }
}

Your call would then change from:

FeatureDecisions.checkEnabledFeatures(...)

to

FeatureDecisions.INSTANCE.checkEnabledFeatures(...)

It's also worth noting that checkEnabledFeatures doesn't actually do anything besides defer to isEnabled . You could scrap the former and just call the latter directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM