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

3,749 questions

4,049 answers

114 comments

10,705 users

Can we use constructor in local inner class?

0 votes
79 views
asked May 11, 2014 by sara Guru (24,570 points)

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 May 11, 2014 by rock Expert (14,990 points)

Related questions

0 votes
1 answer 16 views
0 votes
1 answer 16 views
16 views asked Dec 18, 2013 by john Guru (27,140 points)
0 votes
1 answer 25 views
+1 vote
1 answer 46 views
0 votes
1 answer 36 views
36 views asked Feb 18, 2014 by john Guru (27,140 points)
...