Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the purposes of inner classes

I am reviewing the concept of inner classes in java. so far from what I've understood and applied java inner classes has a link or access to the methods and fields of its outer/ enclosing class.

My Question:

  1. When should create or define an inner class?
  2. are inner classes considered to be called as "Helper classes" ?
  3. What are the indicators for you to make an inner class and what's their other purpose?
like image 221
user962206 Avatar asked Jul 09 '12 15:07

user962206


People also ask

What are the advantages of inner class?

The main advantages of a nested (inner) class are: It shows a special type of relationship, in other words, it has the ability to access all the data members (data members and methods) of the main class including private. They provide easier code because it logically groups classes in only one place.

What are the advantages and disadvantages of inner classes?

Inner classes are used to develop a more readable and maintainable code because they logically group classes and interfaces in one place. Easy access, as the inner object, is implicitly available inside an outer Code optimization requires less code to write. It can avoid having a separate class.

What is an inner class explain with example?

Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a member of other class, then the inner class can be made private. And this is also used to access the private members of a class.

What are some of the essential characteristics of inner classes?

It can access any private instance variable of the outer class. Like any other instance variable, we can have access modifier private, protected, public, and default modifier. Like class, an interface can also be nested and can have access specifiers.


Video Answer


2 Answers

Inner classes are best for the purpose of logically grouping classes that are used in one-place. For example, if you want to create class which is used by ONLY enclosing class, then it doesn't make sense to create a separate file for that. Instead you can add it as "inner class"

As per java tutorial:

Compelling reasons for using nested classes include the following:

  • It is a way of logically grouping classes that are only used in one place.
  • It increases encapsulation.
  • It can lead to more readable and maintainable code.
like image 134
kosa Avatar answered Sep 26 '22 11:09

kosa


A classic use for an inner class is the implementation of an iterator inside a container (ArrayList, for example - look for class Itr). All the container wants to expose to the rest of the world is an Iterator. However, it has to create some concrete implementation of that iterator, possibly familiar with the internals of the container. Using an inner class hides the implementation, while keeping it close to the container's implementation. And being inner (i.e. non-static), it is bound to a specific instance of that container, which lets it access private container members.

There are a few types of inner classes - non-static nested class, local classes and anonymous classes. Each one has a somewhat different purpose, so when asking about an inner class, you should specify what kind are you talking about.

Assuming you're referring to non-static inner classes, I'd say the reason to use them is the same as using regular classes (namely abstraction and dividing code into logical units), but there's no reason to make this use of classes visible to the rest of the world. You can also make nested classes public, of course, in which case you'd make them nested instead of independent in order to express their tight relation with the outer class.

like image 38
eran Avatar answered Sep 26 '22 11:09

eran