search
Login

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

0 votes
92 views
asked Guru 24.6k points 29 52 81

1 Answer

0 votes
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 ");
  }
}
answered Expert 15k points 15 23 30

Related questions

0 votes
1 answer 18 views
0 votes
1 answer 18 views
18 views
asked john Guru 27.1k points 31 50 97
0 votes
1 answer 30 views
1 vote
1 answer 54 views
0 votes
1 answer 39 views
39 views
asked john Guru 27.1k points 31 50 97
...