java - Where to use Local inner classes -
this question has reply here:
advantage of local classes java 3 answerscould please explain me "where need utilize local inner classes"?
example:
public class localtest { int b=30; void display() { class local{ void msg() { system.out.println(b); } } local l = new local(); l.msg(); } public static void main(string args[]) { localtest lt = new localtest(); lt.display(); } }
here local class
local inner class. useful display()
. in type of situations utilize these local inner classes ?
main reason when need implement interface locally , pass somewhere, want add together more methods implementation, anonymous implementation not fit:
public static void main (string [] args) throws exception { class myrunnable extends runnable { private boolean finish = false; public synchronized void waitcomplete () throws interruptedexception { while (!complete) wait (); } @override public void run () { // useful synchronized (this) { finish = true; notifyall (); } } } myrunnable r = new myrunnable (); new thread (r).start (); // in parallel new thread r.waitcomplete (); // forgot thread.join () }
java
No comments:
Post a Comment