java - Why can't I call new from within a static method? -
i have utility functions i've declared static
, not instantiated, used utilities. create generator method can generate objects, in same static
utility context.
public class peerconnection { public class _heartbeat{ protected string beat = "hb_000"; protected string ack = "hb_001"; protected string msg = null; protected date beattime = null; protected date acktime = null; protected short missedbeats = 0; protected short max_miss = 3; } public _heartbeat heartbeat = null; //map of heartbeat objects per peer connection public static list<_heartbeat> heartbeats = new arraylist<_heartbeat>(); public static void generateheartbeat(){ heartbeats.add(new _heartbeat()); }
my reasoning, want call sendheartbeat
method:
private static int sendheartbeat(peerconnection peer){ int acks = 0; peerconnection.generateheartbeat(); peerconnection._heartbeat hb = peer.heartbeats.get(peer.heartbeats.size() - 1); hb.msg = hb.beat; while (acks <= 0 && hb.missedbeats < hb.max_miss){ [...] } }
i concept of why static works way, i'm thinking there has work around scenario.
_heatbeat
not static class, instance of explicitly tied instance of peerconnection
class. i.e. instantiate _heartbeat
need instance of peerconnection
class.
one option make _heartbeat
static class (i.e. public static class _heartbeat
, think want.
another option instantiate both new peerconnection().new _heartbeat()
(i saw in java certification exam, hate , never use may not remembering syntax correctly).
Comments
Post a Comment