Java 访问其他类中一个类的实例时出现的问题

Java 访问其他类中一个类的实例时出现的问题,java,streamsets,Java,Streamsets,我试图访问另一个类中一个类的方法中的实例,并尝试修改它们。但它给了我一个错误,因为“无法引用封闭范围中定义的非最终局部变量nextSourceOffset。它给了我一个将变量修改为final的修复程序。但是如果我将其更改为final,我就无法增加它们 我正在使用以下代码: public String produce(String lastSourceOffset, final int maxBatchSize, BatchMaker batchMaker) throws

我试图访问另一个类中一个类的方法中的实例,并尝试修改它们。但它给了我一个错误,因为“无法引用封闭范围中定义的非最终局部变量nextSourceOffset。它给了我一个将变量修改为final的修复程序。但是如果我将其更改为final,我就无法增加它们

我正在使用以下代码:

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;

                }
            }

        };
公共字符串生成(字符串lastSourceOffset,最终int-maxBatchSize,BatchMaker-BatchMaker)
抛出阶段异常{
long nextSourceOffset=0;
if(lastSourceOffset!=null){
nextSourceOffset=Long.parseLong(lastSourceOffset);
}
final RtmClient client=new RtmClientBuilder(getEndPoint(),getAppKey()).setListener(new RtmClientAdapter()){
@凌驾
公共void OnInterconnected(RtmClient客户端){
}
}).build();
SubscriptionAdapter侦听器=新建SubscriptionAdapter(){
@凌驾
SubscriptionData(SubscriptionData数据)上的公共无效{
int numRecords=0;
for(AnyJson:data.getMessages()){
jsonString=json.toString();
Record Record=getContext().createRecord(“某些id::”+nextSourceOffset);
Map Map=newhashmap();
map.put(“fieldName”,Field.create(jsonString));
record.set(Field.create(map));
batchMaker.addRecord(记录);
++下一个资源补偿;
++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
Record Record=getContext().createRecord(“某些id::”+nextSourceOffset);//错误
Map Map=newhashmap();
map.put(“fieldName”,Field.create(jsonString));
record.set(Field.create(map));
batchMaker.addRecord(记录);//错误
++nextSourceOffset;//错误
++numRecords;//错误

您的内部类无法从外部作用域访问非最终变量。它也无法更改变量的值

最干净的解决方案可能是将所有这些信息封装在一个新类中,并将其存储为属性

快速而肮脏的解决方案是将本地long变量放入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;

            }
        }

    };
公共字符串生成(字符串lastSourceOffset,最终int-maxBatchSize,BatchMaker-BatchMaker)
抛出阶段异常{
long[]nextSourceOffset={0};
if(lastSourceOffset!=null){
nextSourceOffset[0]=Long.parseLong(lastSourceOffset);
}
final RtmClient client=new RtmClientBuilder(getEndPoint(),getAppKey()).setListener(new RtmClientAdapter()){
@凌驾
公共void OnInterconnected(RtmClient客户端){
}
}).build();
SubscriptionAdapter侦听器=新建SubscriptionAdapter(){
@凌驾
SubscriptionData(SubscriptionData数据)上的公共无效{
int numRecords=0;
for(AnyJson:data.getMessages()){
jsonString=json.toString();
Record Record=getContext().createRecord(“某些id::”+nextSourceOffset[0]);
Map Map=newhashmap();
map.put(“fieldName”,Field.create(jsonString));
record.set(Field.create(map));
batchMaker.addRecord(记录);
++nextSourceOffset[0];
++numRecords;
}
}
};