Java 我可以使用其他类中的静态方法吗?
下面是我的Java 我可以使用其他类中的静态方法吗?,java,static,apache-zookeeper,apache-curator,Java,Static,Apache Zookeeper,Apache Curator,下面是我的CuratorClient类,它连接到Zookeeper并开始领导人选举过程 public class CuratorClient { // can I make this as static? private static CuratorFramework client; private String latchPath; private String id; private LeaderLatch leaderLatch; pub
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);
}
}
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;
}
}
一般来说,这不是馆长或动物园管理员的问题。这基本上是一个设计问题,我试图了解我的做法是否会有任何问题?我假设
CuratorFramework
也将是线程安全的?是的,您可以从其他类调用静态方法
您的签名如下所示:
protected static List<String> getChildren(String node) throws Exception
受保护的静态列表getChildren(字符串节点)引发异常
无法从其他类调用此函数的原因是,它是受保护的(对当前类和子类可见),而不是对公共的(对任何地方可见)
如果使其可见,可以使用CuratorClient.getChildren()
调用它
.
.你为什么不呢?实用程序类都是这样构建的。编辑:当然方法和类应该是公共的。您不能从另一个类(不是子类)调用该方法,因为该方法受保护
。protected
方法只能从所属类或所属类的子类调用。当然,如果您能够调用该方法(因为它被声明为public
),它仍将引用CuratorClient
的客户端
静态字段,这可能是你想要的,也可能不是你想要的。@HotLicks:这是我不确定的-CuratorClient的静态字段,这可能是你想要的,也可能不是你想要的。
这让我很困惑。我的主要工作是使用管理员库从Zookeeper获取特定节点的子节点,我正在构建这个实用程序,这样我就可以通过传递需要获取的节点子节点从不同的类调用它。如果不仔细研究应用程序,很难调用。感谢Jeroen的建议。。现在,如果我这样称呼它,那么当它被声明为静态时,我会有任何问题吗?而getChildren
方法将从Zookeeper模式中获取节点的子节点?我的头脑中还没有构建编译器,所以最好的测试方法是执行您的程序,看看会发生什么。在观察结果之后,你可以研究可能的原因并得出结论。将字段设为static
意味着对象的每个实例都将引用static
字段中对象的同一实例。有时这是你想要的,有时不是:这取决于你。我不可能知道getChildren()
会做什么,因为我没有这方面的代码(执行它,你会看到它的功能)。读这篇文章时,它说CuratorFramework
实例是线程安全的。所以我想我可以用它静态的感觉,对吗?请阅读该链接-重要提示:CuratorFramework实例是完全线程安全的。在应用程序中,每个ZooKeeper集群应共享一个CuratorFramework。
protected static List<String> getChildren(String node) throws Exception