Programming Language

Use Of Inner Classes In Java

The inner classes were first introduced in Java 1.1 version. Since the introduction of inner classes, they have inspired several different opinions among people. It is essential to know that when you can use inner classes because the utilization of inner classes in the wrong conditions can result in code that’s not easy to maintain and understand.

An Example Of Where Inner Classes Are Important

Inner classes basically allow you to define one class with another class. This is why they are known as “inner” classes. Below is an example to illustrate that when inner classes are essential.

Suppose you have a Java Graphical User Interface (GUI) class that serves as a chat client, such as Facebook Chat or Gchat. Now think about the methods require to be presented in a class similar to that which shows a chat client. Here you will require a method that is going to read user input from the chat box, for actually sending the user input to whomsoever the user is chatting with and to the server.

Event handlers

However, one important thing is missing here.  How exactly will those procedures be called? So, think about the working of Gchat as an example– Suppose, you just type in some text and ready to press the “Return” or “Enter” key on your keyboard to send it to whomsoever you’re chatting with.

Therefore, the “Enter” key could be regarded as one event that activates a call to the client procedures. And, in case, the chat client class is looking to detect whether someone is typing in a window, then this event will clearly activate the call that someone is typing. Thus, we require some code for detecting when someone is actually typing in the window in real time – essentially when they are pressing a button within their chat window.

Hence, all these events – such as, typing in a window and then pressing the “RETURN” key also needs some procedures for detecting when they occur, and those event handling procedures can then call the suitable chat client procedures.

Leave a Reply