TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Java Inner Class

Java inner class or nested class with member inner class, anonymous inner class, local inner class and java static nested class.

<< Back to JAVA

Java Inner Classes (Nested Classes)

Java inner class or nested class is a class that is declared inside the class or interface.

We use inner classes to logically group classes and interfaces in one place to be more readable and maintainable.

Additionally, it can access all the members of the outer class, including private data members and methods.

Syntax of Inner class

class Java_Outer_class{
 //code
 class Java_Inner_class{
  //code
 }
}

Advantage of Java inner classes

There are three advantages of inner classes in Java. They are as follows:

  1. Nested classes represent a particular type of relationship that is it can access all the members (data members and methods) of the outer class, including private.
  2. Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
  3. Code Optimization: It requires less code to write.

Need of Java Inner class

Sometimes users need to program a class in such a way so that no other class can access it. Therefore, it would be better if you include it within other classes.

If all the class objects are a part of the outer object then it is easier to nest that class inside the outer class. That way all the outer class can access all the objects of the inner class.

Do You Know
  • What is the internal code generated by the compiler for member inner class?
  • What are the two ways to create an anonymous inner class?
  • Can we access the non-final local variable inside the local inner class?
  • How to access the static nested class?
  • Can we define an interface within the class?
  • Can we define a class within the interface?

Difference between nested class and inner class in Java

An inner class is a part of a nested class. Non-static nested classes are known as inner classes.

Types of Nested classes

There are two types of nested classes non-static and static nested classes. The non-static nested classes are also known as inner classes.

  • Non-static nested class (inner class)
    1. Member inner class
    2. Anonymous inner class
    3. Local inner class
  • Static nested class
TypeDescription
Member Inner ClassA class created within class and outside method.
Anonymous Inner ClassA class created for implementing an interface or extending class. The java compiler decides its name.
Local Inner ClassA class was created within the method.
Static Nested ClassA static class was created within the class.
Nested InterfaceAn interface created within class or interface.

Next TopicMember Inner class




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf