java - Adding an object into a static collection for a static method -
probably stupid question, has been driving me nuts days. first of all, i'm speaking code embedded in larger application class , method signature imposed.
so goal create collection of gc information , code following :
public final class jvmmemstats_svc { public static final void jvmmemstats(idata pipeline) throws serviceexception { list<garbagecollectormxbean> gcmbeans = managementfactory.getgarbagecollectormxbeans(); (garbagecollectormxbean gcbean : gcmbeans){ // loop against gcs gc gc = gcs.get(gcbean.getname()); if( gc != null ){ // gc exists } else { // new gc gcs.put( gcbean.getname(), new gc( gcbean.getcollectioncount(), gcbean.getcollectiontime()) ); } } public class gc { public long cnt, duration; public gc(long cnt, long duration){ this.set(cnt, duration); } public void set(long cnt, long duration){ this.cnt = cnt; this.duration = duration; } } static map<string, gc> gcindexes = new hashmap<string, gc>(); } but got following error @ compilation time :
non-static variable cannot referenced static context : gcprev.add( new gc( gcbean.getcollectioncount(), gcbean.getcollectiontime()) ); well ... i'm lost. tip.
laurent
you're trying create instance of non-static inner class gc inside jvmmemstats() static method.
non-static variable cannot referenced static context : gcprev.add( new gc( gcbean.getcollectioncount(), gcbean.getcollectiontime()) ); the static context being referred above jvmmemstats() method. change class declaration to
public static class gc { // needs static instantiated in static method }
Comments
Post a Comment