Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Private Keyword

 The private access modifier is accessible only within the same class.
 We can't assign private to outer class and interface.
 The best use of private keyword is to create a fully encapsulated class in Java by making all the data
members of that class private.
 If we make any class constructor private, we cannot create the instance of that class from outside the
class.
 If we are overriding any method, overridden method (i.e., declared in the subclass) must not be more
restrictive.
 According to the previous point, if we assign a private modifier to any method or variable, that method or
variable can be overridden to sub-class using all type of access modifiers. However, still, we can't invoke
private method outside the class.

Exception
The term exception is shorthand for the phrase "exceptional event."

When an error occurs within a method, the method creates an object and hands it off to the runtime system. The
object, called an exception object, contains information about the error, including its type and the state of the
program when the error occurred. Creating an exception object and handing it to the runtime system is called
throwing an exception.

After a method throws an exception, the runtime system attempts to find something to handle it. The set of
possible "somethings" to handle the exception is the ordered list of methods that had been called to get to the
method where the error occurred. The list of methods is known as the call stack.

How to Write an Action Listener


Action listeners are probably the easiest — and most common — event handlers to implement. You implement an
action listener to define what should be done when an user performs certain operation.

An action event occurs, whenever an action is performed by the user. Examples: When the user clicks a button,
chooses a menu item, presses Enter in a text field. The result is that an actionPerformed message is sent to all action
listeners that are registered on the relevant component.

To write an Action Listener, follow the steps given below:

1. Declare an event handler class and specify that the class either implements an ActionListener interface or
extends a class that implements an ActionListener interface. For example:
2. public class MyClass implements ActionListener {
3. Register an instance of the event handler class as a listener on one or more components. For example:
4. someComponent.addActionListener(instanceOfMyClass);
5. Include code that implements the methods in listener interface. For example:
6. public void actionPerformed(ActionEvent e) {
7. ...//code that reacts to the action...
8. }

In general, to detect when the user clicks an onscreen button (or does the keyboard equivalent), a program must
have an object that implements the ActionListener interface. The program must register this object as an action
listener on the button (the event source), using the addActionListener method. When the user clicks the onscreen
button, the button fires an action event. This results in the invocation of the action listener's actionPerformed
method (the only method in the ActionListener interface). The single argument to the method is an ActionEvent
object that gives information about the event and its source.

You might also like