In processing if you want to register a mouse event listener, you just need to define a function with the name "mousepressed", "mousereleased", etc. and they "magically" become event listeners. This also happens to the controlP5 library I'm using, where all the functions named after a control widget "magically" become its event handler. I'm wondering how does Java handle this kind of magic? Where can I see some source code or topic regarding this pattern. I would like to know its mechanism since I can't define the listeners in main applet.
Processing might use reflection for some stuff, but in the case of the mousePressed() functions, that's a simple matter of inheritance.
Processing contains a PApplet class, the source of which you can view here: https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java
At the time of this answer, line 3087 of the PApplet class is the mousePressed(MouseEvent) function that is called via an Event handler, which you can read about here: http://docs.oracle.com/javase/tutorial/uiswing/events/
This mousePressed(MouseEvent) method calls the no-arg mousePressed() function, which is an empty function on line 3084.
When you write a Processing sketch, you're secretly extending PApplet. When you write a mousePressed() function in your sketch, you override the empty mousePressed() function of the PApplet class. Now when the PApplet class gets a MouseEvent from its MouseListener, it calls your mousePressed function. That's how inheritance works.
If you're asking a more specific question, please provide an MCVE that demonstrates exactly what you're talking about.