0

In the code below, I am trying to access the 'singleBuilding' variable inside the 'GMLWalker' visit function. I am getting the error "Cannot refer to the non-final local variable singleBuilding defined in an enclosing scope". Any clues on how I can achieve this.

private FeatureWalker IterateGroundSurface(BuildingClass singleBuilding){
    FeatureWalker groundWalker = new FeatureWalker(){

        public void visit(GroundSurface groundSurface){
            GMLWalker gmlWalker = new GMLWalker(){
                public void visit(LinearRing linearRing){

                    if(linearRing.isSetPosList()){
                        SurfaceMember surfaceMember = new SurfaceMember();

                        DirectPositionList posList = linearRing.getPosList();
                        List<Double> points = posList.toList3d();

                        List<CoordinateClass> polygonfloor = new ArrayList<CoordinateClass>();
                        PolygonClass poly = new PolygonClass();
                        for(int i=0 ; i<points.size() ;i+=3){
                            double[] vals = new double[]{points.get(i) , points.get(i+1),points.get(i+2)};
                            //System.out.println(vals[0]+" "+vals[1]+" "+vals[2]);
                            CoordinateClass coord = new CoordinateClass(vals);
                            polygonfloor.add(coord);
                        }
                        poly.setPolygon(polygonfloor);
                        surfaceMember.setPolygon(poly);
                        singleBuilding.setSurfacePolygon(surfaceMember);


                        //surfacePolygons.add(buildingSurfacePolygon);
                    }
                }
            };
            groundSurface.accept(gmlWalker);
        }
    };
    return groundWalker;
}

Thanks,

zelta
  • 105
  • 2
  • 9
  • Java doesn't support true closures (at least pre Java 8). [This thread](http://stackoverflow.com/questions/1299837/cannot-refer-to-a-non-final-variable-inside-an-inner-class-defined-in-a-differen) explains your problem in depth. – Mick Mnemonic Apr 26 '15 at 13:23
  • Thanks for the answer, looks like I will have to use global variables. – zelta Apr 26 '15 at 14:01

1 Answers1

0

You need to make it final if you want to access it in a inner class. Again if you make it final then you won't be able to re initialize it.

private FeatureWalker IterateGroundSurface(final BuildingClass singleBuilding){
}
Eranda
  • 1,439
  • 1
  • 17
  • 30
  • If you need to know why then you can follow @Mick Mnemonic's comment. – Eranda Apr 26 '15 at 13:27
  • That's an issue. I have to keep updating the singleBuilding variable. As of a temp solution I am using a global variable, to handle this. I looked at this: http://stackoverflow.com/questions/7244511/anonymous-or-real-class-definition-when-using-visitor-pattern . But I thinks its again, using the global variable approach. – zelta Apr 26 '15 at 13:59