简体   繁体   English

我可以使用其他类的静态方法吗?

[英]Can I use static method from a different class?

Below is my CuratorClient class which is connecting to Zookeeper and starting the leader election process as well. 下面是我的CuratorClient类,该类正在连接到Zookeeper并也开始执行领导者选举过程。

public class CuratorClient {

    // can I make this as static?
    private static CuratorFramework client;
    private String latchPath;
    private String id;
    private LeaderLatch leaderLatch;

    public CuratorClient(String connString, String latchPath, String id) {
        client = CuratorFrameworkFactory.newClient(connString, new ExponentialBackoffRetry(1000, Integer.MAX_VALUE));
        this.id = id;
        this.latchPath = latchPath;
    }

    public void start() throws Exception {
        client.start();
        client.getCuratorClient().blockUntilConnectedOrTimedOut();
        leaderLatch = new LeaderLatch(client, latchPath, id);
        leaderLatch.start();
    }

    public boolean isLeader() {
        return leaderLatch.hasLeadership();
    }

    public Participant currentLeader() throws Exception {
        return leaderLatch.getLeader();
    }

    public void close() throws IOException {
        leaderLatch.close();
        client.close();
    }

    // can I use below method from any other class ?
    protected static List<String> getChildren(String node) throws Exception {
        return client.getChildren().forPath(node);
    }
}

When my service gets started up, in the static block I am making a connection to Zookeeper using CuratorClient and starting the leader election process as well. 当我的服务启动时,在静态块中,我正在使用CuratorClient与Zookeeper建立连接,并也开始了领导者选举过程。

public class TestService {

    private static CuratorClient curatorClient = null;
    static {

        try {
            String connectionString = "some-string";
            String hostname = "machineA";

            curatorClient = new CuratorClient(connectionString, "/my/latch", hostname);
            curatorClient.start();

        } catch (Exception ex) {
            // log exception
        }
    }

    ....
    ....

    // some method
    public Map<String, String> installNewSoftware(String node) {

    //.. some other code
    try {
        List<String> children = CuratorClient.getChildren("/my/example");
        System.out.println(children);

    } catch (Exception e) {
        e.printStackTrace();
    }

    //.. some other code
    return null;
    }
}

Now I have some other class as well which likes to use the getChildren method of CuratorClient so in this class, I can directly use like this CuratorClient.getChildren("/my/example"); 现在,我还有其他一些类喜欢使用CuratorClientgetChildren方法,因此在此类中,我可以像这样直接使用CuratorClient.getChildren("/my/example"); correct? 正确?

public class DifferentClass {

    ....
    ....

    // some new method
    public Map<String, String> installNewSoftware(String node) {
    try {
        List<String> children = CuratorClient.getChildren("/my/example");
        System.out.println(children);

    } catch (Exception e) {
        e.printStackTrace();
    }

    //.. some other code
    return null;
    }
}

In general, this is not a curator question or zookeeper question. 通常,这不是策展人问题或动物园管理员问题。 It's basically a design question and I am trying to understand whether the way I am doing it will have any problem or not? 这基本上是一个设计问题,我正在尝试了解我的操作方式是否会出现问题? And I am assuming CuratorFramework will be thread safe as well? 我假设CuratorFramework也将是线程安全的吗?

Yes, you can call static methods from other classes. 是的,您可以从其他类调用static方法。

Your signature looks like this: 您的签名如下所示:

protected static List<String> getChildren(String node) throws Exception

The reason you can't call this from another class is because it's protected (visible to the current class and subclasses) instead of public (visible to everywhere). 之所以不能从另一个类中调用它,是因为它是protected (对当前类和子类可见),而不是public (对任何地方都可见)。

If you make it visible you can call it with CuratorClient.getChildren() . 如果使其可见,则可以使用CuratorClient.getChildren()进行调用。

More information on access modifiers . 有关访问修饰符的更多信息
More information on class members ( static fields) . 有关类成员的更多信息( static字段)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用来自不同类中的类的方法? - How can I use a Method from a class in a different class? 我如何在同一个 class 的不同方法中使用私有 static 方法(对象的扩展)的返回值? (爪哇) - How can i use the return value of a private static method (extension of object) in a different method in that same class? (java) 如何在其他类中使用方法? - How can I use a method in a different class? 使用其他类的公共静态HashTable的同步方法。 如何使该方法线程安全? - synchronized method that use a public static HashTable from other class. How can I make this method thread safe? 我可以使用添加到ArrayList的其他类中的方法吗? - Can I use a method from a different class that I have added to an ArrayList? 我们可以在抽象类中使用静态方法吗? - Can we use static method in an abstract class? 为什么我不能使用已实现接口的静态方法? - Why can't I use from the static method of the implemented interface? 我可以使用静态块中的字段并将其分配给类字段吗? - Can I use a field from a static block and assign it to the class field? 从静态调用非静态方法(在不同的类中) - Calling non-static method (in a different class) from static 如何从一个方法更改静态类变量的值,然后从另一个方法获取它呢? - How can I change the value of a static class variable from a method, then get it from an other method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM