wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> cs >> Java code doubts
(Message started by: mistaken_id on Jul 26th, 2009, 10:57pm)

Title: Java code doubts
Post by mistaken_id on Jul 26th, 2009, 10:57pm
public FolderWatcher() {


Pref.Bool.WatchFS.evtChanged.add(new Event.Listener<Boolean> () {



public void update(Boolean eventData) {




/*




* FIXME Save preferences and scope registry before we remove the watches;




* on Windows that may cause a crash.




*/




if (! eventData)





try {






Pref.save();






ScopeRegistry.getInstance().save();





} catch (IOException e) {





}




setThreadWatchEnabled(eventData);



}


});

What does this code do ? Could anyone tell me the high level description of this code. I DONT what the exace detaisl of this code

Pref.Bool.WatchFS.evtChanged.add is a method .........the above code calls this method. However, instead of passing arguments to the method, a block of code is defined, Is there any term in Java for tihs case. Could anyone tell me what this is called


Title: Re: Java code doubts
Post by mistaken_id on Jul 26th, 2009, 11:01pm
JNotifyListener fsListener = new JNotifyListener() {
public void fileCreated(int arg0, String arg1, String arg2) {
handleEvent(arg1, arg2);
}
public void fileDeleted(int arg0, String arg1, String arg2) {
handleEvent(arg1, arg2);
}
public void fileModified(int arg0, String arg1, String arg2) {
handleEvent(arg1, arg2);
}
public void fileRenamed(int arg0, String arg1, String arg2, String arg3) {
handleEvent(arg1, arg2);
}
};
And similarly this code.... it is creating an object...how ever a block of code follows it...what does this mean

Title: Re: Java code doubts
Post by Grimbal on Jul 27th, 2009, 4:20am
It is an anonymous inner class.  It is a way to define a subclass of a class right where it is used.
In this case, it means you are creating a new instance of a subclass of JNotifyListener, but overriding some of its procedures.

It is equivalent to the following


public void someMethod() {
...
// this class has access to some of someMethod's local variables
class MyListener extends JNotifyListener {
 // override these
 public void fileCreated(int arg0, String arg1, String arg2) {
  handleEvent(arg1, arg2);
 }
 ... (other methods)
}

...
// create and use an instance of the listener
JNotifyListener fsListener = new MyListener();
// for instance
someObject.addListener(fsListener);
}

Title: Re: Java code doubts
Post by meetlesli on Jul 19th, 2014, 11:09am
Thanks Grimbal .. it worked for me



Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board