2017-08-22 4 views
-1

別のクラス内のあるクラスのメソッドでインスタンスにアクセスしようとしていて、それらを変更しようとしています。しかし、それは私に "としてエラーをスローされている包括的なscope.Andで定義されている非最終ローカル変数nextSourceOffsetを参照することはできません最終"に変数を変更する修正を与えている "私はそれらを増やすことはできません。他のクラス内のあるクラスのインスタンスにアクセスする際の問題

私は、次のコードを使用しています:

public String produce(String lastSourceOffset, final int maxBatchSize, BatchMaker batchMaker) 
      throws StageException { 
     long nextSourceOffset = 0; 
     if (lastSourceOffset != null) { 
      nextSourceOffset = Long.parseLong(lastSourceOffset); 
     } 
     final RtmClient client = new RtmClientBuilder(getEndPoint(), getAppKey()).setListener(new RtmClientAdapter() { 
      @Override 
      public void onEnterConnected(RtmClient client) { 

      } 
     }).build(); 
     SubscriptionAdapter listener = new SubscriptionAdapter() { 

      @Override 
      public void onSubscriptionData(SubscriptionData data) { 
       int numRecords = 0; 
       for (AnyJson json : data.getMessages()) { 
        jsonString = json.toString(); 
        Record record = getContext().createRecord("some-id::" + nextSourceOffset); 
        Map<String, Field> map = new HashMap<>(); 
        map.put("fieldName", Field.create(jsonString)); 
        record.set(Field.create(map)); 
        batchMaker.addRecord(record); 
        ++nextSourceOffset; 
        ++numRecords; 

       } 
      } 

     }; 

それは時にエラーを投げている:

Record record = getContext().createRecord("some-id::" + nextSourceOffset);//error 
        Map<String, Field> map = new HashMap<>(); 
        map.put("fieldName", Field.create(jsonString)); 
        record.set(Field.create(map)); 
        batchMaker.addRecord(record);//error 
        ++nextSourceOffset;//error 
        ++numRecords;//error 
+0

可能重複[囲む範囲で定義された非最終的なローカル変数ボタンを参照できないため、ランダムな方法エラー(https://stackoverflow.com/questions/32305839/cannot - 非最終ローカル変数を基準とするボタンを囲むスコープで定義された) –

答えて

1

あなたの内部クラスは、外部の範囲から非final変数にアクセスすることはできません。変数の値も変更できません。

最もクリーンな解決策は、おそらくこのすべての情報を新しいクラスにカプセル化し、属性として格納することです。

ローカルのロング変数をlong型の単一要素配列にすることが短時間で解決できます。常に同じ配列を参照するので、変数自体は最終的ですが、配列の内容は変更することができます。

例:

public String produce(String lastSourceOffset, final int maxBatchSize, BatchMaker batchMaker) 
     throws StageException { 
    long[] nextSourceOffset = {0}; 
    if (lastSourceOffset != null) { 
     nextSourceOffset[0] = Long.parseLong(lastSourceOffset); 
    } 
    final RtmClient client = new RtmClientBuilder(getEndPoint(), getAppKey()).setListener(new RtmClientAdapter() { 
     @Override 
     public void onEnterConnected(RtmClient client) { 

     } 
    }).build(); 
    SubscriptionAdapter listener = new SubscriptionAdapter() { 

     @Override 
     public void onSubscriptionData(SubscriptionData data) { 
      int numRecords = 0; 
      for (AnyJson json : data.getMessages()) { 
       jsonString = json.toString(); 
       Record record = getContext().createRecord("some-id::" + nextSourceOffset[0]); 
       Map<String, Field> map = new HashMap<>(); 
       map.put("fieldName", Field.create(jsonString)); 
       record.set(Field.create(map)); 
       batchMaker.addRecord(record); 
       ++nextSourceOffset[0]; 
       ++numRecords; 

      } 
     } 

    }; 
関連する問題