Java 我需要在不同于具有构造函数的类的类中实现静态计数器

Java 我需要在不同于具有构造函数的类的类中实现静态计数器,java,Java,我需要为一个P2P网络中的节点ID创建静态计数器。现在节点变成了ID,但我需要更改它,因为我需要每个节点的确切ID。节点1必须具有ID=1,以此类推。我的问题是计数器必须不在类中,节点的构造函数在类中 所以我的柜台必须在班级糕点餐厅 public class PastryNodeFactory implements ComponentFactory { @Override public Component createComponent(Host host) { PastryNode n

我需要为一个P2P网络中的节点ID创建静态计数器。现在节点变成了ID,但我需要更改它,因为我需要每个节点的确切ID。节点1必须具有ID=1,以此类推。我的问题是计数器必须不在类中,节点的构造函数在类中

所以我的柜台必须在班级糕点餐厅

public class PastryNodeFactory implements ComponentFactory {
@Override
public Component createComponent(Host host) {
    PastryNode node = new PastryNode(host.getTransLayer());
    return node;
}
}
下面是我的PastryNode类:

public class PastryNode extends AbstractOverlayNode<PastryID, PastryContact>
    implements DHTNode<PastryID, PastryContact, PastryKey> {

private static Logger log = SimLogger.getLogger(PastryNode.class);

/**
 * The transport layer of the node
 */
private TransLayer transLayer;

/**
 * The contact of the node
 */
private final PastryContact localContact;

/**
 * The message handler of this node
 */
protected PastryMessageHandler msgHandler;

/**
 * The leaf set of the node
 */
protected LeafSet leafSet;

/**
 * The neighborhood set of the node
 */
protected NeighborhoodSet neighborhoodSet;

/**
 * Tells whether this node is in a state where it should be present in the
 * overlay. This means, it either is present or absent due to churn. This
 * flag is needed to decide if a node should initiate a automatic join after
 * a churn event. This way, absent nodes stay absent after a churn event.
 */
private boolean wantsToBePresent = false;

/**
 * Holds pending operations
 */
protected final Map<Integer, AbstractPastryOperation<?>> registeredOperations = new LinkedHashMap<Integer, AbstractPastryOperation<?>>();

/**
 * A map that contains all stored DHTObjects of this node, wrapped in an
 * instance of DHTListener. This allows for later addition of other
 * DHT-Related services.
 */
private DHTListener<PastryKey> dht = new SimpleDHTService<PastryKey>();

private KBRLookupProvider<PastryID, PastryContact, PastryKey> kbrProvider;

private KBRListener<PastryID, PastryContact, PastryKey> kbrListener;

// private final Map<PastryKey, DHTObject> storedObjects;

protected PastryNode(TransLayer translayer) {
    super(new PastryID(
            translayer.getLocalTransInfo(PastryConstants.PASTRY_PORT)),
            PastryConstants.PASTRY_PORT);

    this.transLayer = translayer;

    localContact = new PastryContact(getOverlayID(), getTransLayer()
            .getLocalTransInfo(PastryConstants.PASTRY_PORT));
公共类PastryNode扩展AbstractOverlyNode
实现DHTNode{
私有静态记录器log=SimLogger.getLogger(PastryNode.class);
/**
*节点的传输层
*/
私有跨层跨层;
/**
*节点的接触点
*/
私人最终PastryContact本地联系人;
/**
*此节点的消息处理程序
*/
受保护的PastryMessageHandler msgHandler;
/**
*节点的叶集
*/
保护套叶;
/**
*节点的邻域集
*/
受保护的邻域集;
/**
*说明此节点是否处于应在中出现的状态
*覆盖。这意味着,由于搅动,它要么存在,要么不存在。这
*需要使用标志来决定节点是否应在
*搅动事件。这样,在搅动事件之后,缺席的节点保持缺席状态。
*/
私有布尔值wantsToBePresent=false;
/**
*保留挂起的操作
*/
受保护的最终地图>();
/**
*包含此节点的所有存储DHTObjects的映射,用
*DHTListener的实例。这允许以后添加其他
*DHT相关服务。
*/
private DHTListener dht=new SimpleDHTService();
私人KBRLookupProvider kbrProvider;
私人KBRListener KBRListener;
//私人最终地图存储对象;
受保护的糕点节(跨层-跨层){
超级(新糕点)(
translayer.getLocalTransInfo(PastryConstants.PASTRY_PORT)),
糕点常数。糕点(端口);
this.transLayer=transLayer;
localContact=newPastryContact(getOverlayID(),getTransLayer())
.getLocalTransInfo(PastryConstants.PASTRY_PORT));

你能帮我一下吗?

如果你真的想在P2P网络上实现这一点,就我所知不会这么容易。你是如何处理P2P连接的?对不起,我没有提供足够的信息。我正在使用名为peerfactsim.kom的P2P模拟器。糕点覆盖是其中的一部分,我只需添加计数器。