EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Inner Class

By Savi JaggaSavi Jagga

Home » Software Development » Software Development Tutorials » Software Development Basics » Inner Class

Inner Class

Definition of Inner Class

A class that is present inside another class or block and is treated as one of its members is known as an inner class. It is also known as a nested class and can easily access all the outer class/block members. This helps one declare a user-defined data type local to a block to implement a special type of relationship and helps to write more readable and maintainable code. It can be classified into 2 different categories:-

  1. Static Nested Classes: If a nested class is declared as static.
  2. Inner Classes: Non-static nested classes is known as an inner class.

Categories of Inner Class

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Why do We Need Inner Class?

One needs to use inner class in the application when:-

  • Local Class: If a class is used by only one other class, then it is more preferable to put the single-use class as a local inner class. Such embedding of a class with its helper class results in a better-streamlined package.
  • Optimized Code: Such arrangement of code makes our code more optimized and more efficient memory usage wise.
  • Readable and Maintainable Code: Such an arrangement of classes where a class is placed closer to the place where it is used results in easily readable code and looks maintained.

How does Inner Class Work in Programming languages?

Let’s understand each of the above one by one:-

1. Static Nested

This type of static class is declared inside another class and is treated as one of its members. Since it is static thus can be easily called by other static members without initiating the outer class. And being static, it does not have access to other non-static instance members of the outer class.

Syntax:

class Outer{
public static class Inner{
}
}

2. Method Local

This class type is declared inside a method and is treated similarly to its local methods and variables. And since it is a local member thus can not be accessed outside the method. To use it, one needs to create its instance inside the method itself.

Note: Here, instance variables of the Outer class are not accessible until they are declared as final upto JDk 1.7. In JDK 1.8, this flaw was overcome.

Syntax:

class Outer{
public int method1(){
//task to be performed
public class Inner{
//variables and methods of inner class
}
}
}

Popular Course in this category
Sale
All in One Software Development Bundle (600+ Courses, 50+ projects)600+ Online Courses | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (3,144 ratings)
Course Price

View Course

Related Courses
Software Testing Training (9 Courses, 2 Projects)Selenium Automation Testing Training (9 Courses, 4+ Projects, 4 Quizzes)Appium Training (2 Courses)JMeter Testing Training (3 Courses)

3. Anonymous

This type of classes is declared without a name. Such classes are used when one needs to override a method of a class or an interface. In this case, declaration of the class and instantiation needs to be implemented at the same time.

Syntax:

AnonymousInner an_inner = new AnonymousInner() {
public void my_method() {
........
........
}
};

4. Inner Class

This type of classes is implemented by declaring a class inside another class where the inner class has access to all the instance variables and the outer class’s methods. Since inner class here is associated with an instance, thus must never be defined as static members.

Syntax:

class Outer{
class Inner{
}
}

Examples of Inner Class

Following are the example are given below:

Example #1 – Static Nested

Code:

package Try;
public class Office {
static class Admin {
public void my_method() {
System.out.println("Admin name is Sarthak");
}
}
public static void main(String args[]) {
Office.Admin nested = new Office.Admin();
nested.my_method();
}
}

Output:

Inner Class-1.1

Example #2 – Method Local

Code:

package Try;
public class Office {
void my_Method() {
final int  numOfDesk = 23;
class Inner {
public void print() {
System.out.println("Number of desk in Office =  "+ numOfDesk);
}
}
Inner inner = new Inner();
inner.print();
}
public static void main(String args[]) {
Office outer = new Office();
outer.my_Method();
}
}

Output:

Inner Class-1.2

Example #3 – Anonymous

Code:

package Try;
abstract class MyInner {
public abstract void myMethod();
}
public class Office {
public static void main(String args[]) {
MyInner my_inner = new MyInner() {
public void myMethod() {
System.out.println(“Implementing myMethod named abstract method");
}
};
my_inner.myMethod();
}
}

Output:

Output-1.3

Example #4 – Inner Class

Code:

package Try;
class Company {
class MyCEO {
public void show() {
System.out.println("CEO of this institute is Mr. Rakesh Kumar");
}
}
}
public class Office {
public static void main(String[] args) {
Company.MyCEO in = new Company().new MyCEO();
in.show();
}
}

Output:

Output-1.4

Advantages

  • Maintainable Code: It places a one-time use class inside the other class that enhances the readability and maintenance of the code; thus, our application becomes more maintainable.
  • Enhances Encapsulation: Encapsulation is one of the properties of Object-Oriented Programming Languages where related variables and methods are encapsulated under one block named as a class and are referred to using an instance of that class. It is implemented as a part of the outer class and is treated as a member of the class, thus enhances encapsulation in our code, thus making it more efficient and maintainable.
  • Improves Optimization: Using this in one’s application helps the compiler compile the class easily, thus enhancing the optimization of the code. Also, memory and CPU utilization by the compiler is reduced as less memory is utilized for a new class.

Conclusion

An inner or nested class is a way to place the classes used only at one place in our code closer. This enhances the code’s encapsulation and optimisation and makes our code more readable and maintainable as the members are made more accessible from other classes.

Recommended Articles

This is a guide to Inner Class. Here we also discuss how does inner classwork in programming languages? Along with different examples and their code implementation. you may also have a look at the following articles to learn more –

  1. Inner Class in Java
  2. Anonymous Inner Class in Java
  3. Anonymous Class in Java
  4. SQL Inner Join

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Programming Languages Tutorial
  • Programming Languages Basics
    • Algorithm in Programming
    • Web Development Apps in Go Programming
    • Back End Programming Languages
    • Best Programming Languages
    • Code Generator Tools
    • Duck Number
    • Smith Number
    • Evil Number
    • Mystery Number
    • clock_gettime
    • Ionic List
    • Career in Computer Programming
    • Scheme Programming Language
    • Concept Programming Language
    • Programming for beginners
    • Anaconda version
    • What is Character
    • Clojure
    • What is a Programming Language
    • What is Inheritance in Programming
    • Computer Programming Language
    • Scratch Programming Examples
    • What is Threading
    • What is Type Casting
    • Learning Algorithms
    • CodeIgniter Interview Questions
    • What is CodeIgniter
    • Struts 2 Interview Questions
    • What is App Stack
    • Joomla Version
    • F# Programming
    • Sqoop
    • Raspberry Pi with a Mix of Python
    • Advantages Of Array
    • Advantages of OOP
    • Polymorphism Advantages
    • Introduction To Algorithm
    • What is an Algorithm
    • What is VBScript
    • VBScript Commands
    • VBScript String Functions
    • clock() Function
    • Coq Language
    • Loops in VBScript
    • Composition in OOPS
    • Lua while Loop
    • Encapsulation?in OOPs
    • Association in OOPs
    • Polymorphism in OOPS
    • Encapsulation Types
    • Front End languages
    • Flutter Architecture
    • Flutter Layout
    • Inner Class
    • FoxPro programming
    • Overriding in OOPs
    • Abstraction in OOPs
    • Merge Sort Algorithm
    • JSON Pretty
    • Web Programming Languages
    • What is NetBeans?
    • Array Implementation of Stack
    • Bubble Sort Algorithm
    • Clojure loop
    • OCaml Map
    • JDBC Driver
  • Lua
    • Lua programming?
    • Lua?Comment
    • Lua Variables
    • Lua print
    • Lua Boolean
    • Lua?not equal
    • Lua and
    • Lua Modulo
    • Lua or
    • Lua else if
    • Lua Loop
    • Lua for loop
    • Lua Break
    • Lua continue
    • Lua goto
    • Lua String Format
    • Lua String Concatenation
    • Lua Array
    • Lua array length
    • Lua Classes?
    • Lua require
    • Lua?assert
    • Lua Table
    • Lua print?table
    • Lua Table Length
    • Lua If
    • Lua Next
    • Lua Table Insert
    • Lua?iterate table
    • Lua math
    • Lua list
    • Lua return
    • Lua userdata
    • Lua Metatable
    • Lua Round
    • Lua wait
    • Lua random
    • Lua HTTP
    • Lua JSON
    • Lua sleep
    • Lua RegEx
    • Lua tonumber
    • Lua?table to string
    • Lua File
    • Lua write to file
  • Haskell
    • Haskell Programming Language
    • Haskell Newtype
    • Haskell Programming
    • Haskell Uses
    • Haskell Library
    • Haskell not
    • Haskell not equal
    • Haskell or
    • Haskell either
    • Haskell if
    • Haskell String
    • Haskell ByteString
    • Haskell Array
    • Haskell function
    • Haskell vector
    • Haskell maybe
    • Haskell Where
    • Haskell tuple
    • Haskell Map
    • Haskell Sort
    • Haskell?quicksort
    • Haskell set
    • Haskell Filter Function
    • Haskell let
    • Haskell applicative
    • Haskell do notation
    • Haskell prelude
    • Haskell take function
    • Haskell list
    • Haskell list comprehension
    • Haskell pattern matching
    • Haskell read
    • Haskell Stack
    • Haskell zip
    • Haskell zipWith
  • Flask
    • Flask Version
    • Flask make_response
    • Flask WebSocket
    • Flask Session
    • Flask Environment Variables
    • Flask Cache
    • Flask Server
    • Flask jsonify
    • Flask logging
    • Flask redirect
    • Flask wtforms
    • Flask config
    • Flask Users
    • Flask upload file
    • Flask?get post data
    • Flask Template
    • Flask DB Migrate
    • Flask HTTPS
    • Flask bcrypt
    • Flask debug mode
    • Flask authentication
    • Flask Migrate
    • Flask URL Parameters
    • Flask API
    • Flask bootstrap
    • Flask POST request
    • Flask Extensions
  • OpenCV
    • OpenCV Version
    • OpenCV erosion
    • OpenCV ellipse
    • Open CV resize()
    • OpenCV bitwise_and
    • OpenCV Normalize
    • OpenCV ApproxPolyDP
    • OpenCV SIFT
    • OpenCV?HSV range
    • OpenCV text
    • OpenCV cvtColor
    • OpenCV perspectivetransform
    • OpenCV flip()
    • OpenCV Histogram
    • OpenCV kmeans
    • OpenCV rotate image
    • OpenCV findContours
    • OpenCV Get Image Size
    • OpenCV Line Detection
    • OpenCV kalman filter
    • OpenCV read image
    • OpenCV putText
    • OpenCV Line
    • OpenCV HSV
    • OpenCV Hough Transform
    • OpenCV ORB
    • OpenCV warpPerspective()
    • OpenCV Mat
    • OpenCV rectangle
    • OpenCV Threshold
    • OpenCV inRange
    • OpenCV circle
    • OpenCV imshow
    • OpenCV crop image
    • OpenCV watershed
    • OpenCV bounding box
    • OpenCV imread
    • OpenCV dilate
    • OpenCV imwrite
    • OpenCV filter2d
    • OpenCV Median Filter
    • OpenCV Morphology
    • OpenCV background substration
    • OpenCV haar Cascade
    • OpenCV Load Image
    • Open CV rect ()
    • OpenCV GUI
    • OpenCV sobel operator()
    • OpenCV addWeighted
    • OpenCV drawcontours
    • OpenCV findhomography()
    • OpenCV Gaussian Blur
    • OpenCV Histogram Equalization
    • OpenCV KeyPoint
    • OpenCV namedWindow
    • OpenCV Optical Flow
    • OpenCV remap
    • OpenCV waitKey
    • OpenCV?scale image
    • OpenCV matchTemplate
    • OpenCV contrib
    • OpenCV OCR
  • Rust
    • What is Rust Programming
    • Rust Web Framework
    • Rust Server
    • Rust Library
    • Rust Admin Commands
    • Rust for loop
    • Rust enum
    • Rust?Array
    • Rust Set
    • Rust zip
    • Rust string
    • Rust usize
    • Rust HashMap
    • Rust hashset
    • Rust Iterator
    • Rust Queue
    • Rust XML
  • Prolog
    • Prolog Programming
    • Prolog Operator
    • Prolog Comments
    • Prolog?list
    • Prolog Reverse
    • Prolog length
    • Prolog setof
    • Prolog if
    • Prolog mod
    • Prolog findall
    • Prolog predicate
    • Prolog converge
    • Prolog or
    • Prolog print
    • Prolog write
    • Prolog not equal
    • Prolog append
    • Prolog Reverse List
    • Prolog Factorial
    • Prolog Recursion
    • Prolog not
    • Prolog Unification
    • Prolog Family Tree
    • Prolog trace
    • Prolog assert
    • Prolog cut
    • Prolog XML

Related Courses

Python Training Certification

Online Java Course

Programming Languages Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) Learn More