ProAnswers.org

Can we use constructor in local inner class?

yes, The local inner class may have constructor.

interface Counter {
int next();
}

public class MainClass{

private int count = 0;
Counter getCounter(final String name) {
// A local inner class:
class LocalCounter implements Counter {
public LocalCounter() {
// Local inner class can have a constructor
System.out.println(“LocalCounter()”);
}
public int next() {
System.out.print(name); // Access local final
return count++;
}
}
return new LocalCounter();
}

public static void main(String[] args) {
MainClass lic = new MainClass();
Counter c1 = lic.getCounter("Local inner ");
}
}