Reputation: 323
In my following code i used j variable which is non-final local variable
for (int j = 0; j < 4; j++) {
if (videoCapture[j].isOpened()) {
panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
mat = new Mat();
videoCapture[j].read(mat);
BufferedImage image = Mat2BufferedImage(mat);
g.drawImage(image, 0, 0, this);
repaint();
}
};
}
In my above code I can't use j in videoCapture[j].read(mat)
. When I use j, it give error Can not refer to the non local variable j defined in an enclosing scope. If I declare j as final then j can't be increment. Here I need to use j, so any one can help me to resolve this problem?
Upvotes: 1
Views: 203
Reputation: 36304
There is a workaround. Use a temp variable. Like this :
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
final int val = i; // temp final variable
new Thread() {
public void run() {
System.out.println(val); //the compiler checks for val (and according to javac rules, val should be (and it is) final).
};
}.start();
}
}
Upvotes: 3