2013-05-18 3 views
1

아마도 바보 같은 질문 일지 몰라도 며칠 동안 나를 괴롭 히고 있습니다. 우선, 더 큰 응용 프로그램에 포함 된 코드에 대해 이야기하고 있으므로 클래스와 메서드 서명이 부과됩니다. 정적 메서드에 대한 정적 컬렉션에 개체 추가

그래서 내 목표는 GC 정보의 컬렉션을 만드는 것입니다 및 코드는 다음과 같다 :

public final class JVMMemStats_SVC { 
    public static final void JVMMemStats(IData pipeline) throws ServiceException { 
     List<GarbageCollectorMXBean> gcMBeans = ManagementFactory.getGarbageCollectorMXBeans(); 
     for (GarbageCollectorMXBean gcBean : gcMBeans){ // Loop against GCs 
      GC gc = GCs.get(gcBean.getName()); 
      if(gc != null){ // This GC already 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>(); 
} 

하지만 컴파일시 다음과 같은 오류 있어요 : 음

non-static variable this cannot be referenced from a static context : 
    GCPrev.add(new GC(gcBean.getCollectionCount(), gcBean.getCollectionTime())); 

을 ... 나는 길을 잃었다. 모든 팁 주셔서 감사.

랑은

+0

으로 변경하십시오. GCPrev 란 무엇입니까? – Bathsheba

답변

0

정적 메서드 JVMMemStats() 내부에 non-static 내부 클래스 GC의 인스턴스를 만들려고합니다.

non-static variable this cannot be referenced from a static context : 
    GCPrev.add(new GC(gcBean.getCollectionCount(), gcBean.getCollectionTime())); 

위에서 언급 한 정적 컨텍스트는 JVMMemStats() 메서드입니다. 클래스 선언을

public static class GC { 
    // needs to be static to be instantiated in a static method 
} 
+0

감사합니다. Ravi, 잘 작동합니다. – destroyedlolo

+0

@destroyedlolo 잘 작동했기 때문에 기쁩니다. 도움이 되었다면 답변을 수락하는 것을 고려해보십시오. 감사. –

+0

끝났습니다. 다시 한 번 감사드립니다. – destroyedlolo

0

비 정적 변수, 정적 메소드 mathods 액세스 할 수 없다. 따라서 정적 변수를 비 정적 변수로 변경하거나 비 정적 메서드를 정적 변수로 변경하고 검사하십시오.

관련 문제