2

What is the exact purpose of inner classes in Java and creating methods in it. Can I get the same behavior if I create methods in a class instead of creating those methods inside Inner class?

We can access both Inner class methods as well as instance methods outside of outer class. So what is the exact purpose of Inner classes in Java? Is there any situation/possibility where we can't survive without inner classes?

2
  • 1
    You could ever survive without inner classes, but they make much things easier - especially if you want to have two classes that uses the same variables but one if it is only used by the other class, or if you want to have an ActionListener without defining a ne class for it (in this case use an anonymos inner class) Commented Sep 7, 2014 at 12:10
  • It's another way to organise your code, with closely related classes together. Inner classes are more useful if the caller doesn't need to know the class. e.g. collection's use of nested classes. Commented Sep 7, 2014 at 14:20

3 Answers 3

2

Using them greatly depends on what you need to do. Sometimes you need a class that will be only used inside of one particular class, sometimes you need to quickly create an instance of Comparator and pass it to sort() method (anonymous inner class). Some inner classes are accessible outside of the class, some aren't. There are four different varieties of nested classes in java, for more I recommend reading this: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Sign up to request clarification or add additional context in comments.

2 Comments

There are four varieties of nested class.
Instead of creating of that class inside of another class, why can't we manage with the local/static methods.To access instance methods we need object of that class, same time to access inner class methods we need outer class object. So what is the point of creating inner classes here.
0

Inner class does not instantiate upon outer class instantiation unless you explicitly do it in the constructor. Therefore it's methods are not useful to the rest of the class unless they are static or you have created an instance of it and using them.

If you place your methods inside the inner class they can access the inner method, instance variables! You cannot access those variables in the outer class.

Comments

0

you can refer to this

basically inner class keep the object oriented programming more intact. Also sometime when you have to implement an interface which has only one unimplemented method inner class comes handy. specially anonymous inner class. also instead of writing complicated if-else structure or switch case we can use this inner classes for callbacks

1 Comment

I won't suggest Anonymos Inner Classes for Interfaces with only one method anymore. Use Java8's Lambdas instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.