123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Computer-Programming >> View Article

Why We Used Inner Classes In Java

Profile Picture
By Author: Infocampus
Total Articles: 33
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

Nested Classes :
In Java , just like methods, variables of a class too can have another class as its member. Writing a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class.
Inner Classes (Non-static Nested Classes) :
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. Best core java training in bangalore And this is also used to access the private members of a class.
Types Of Inner Class :
Inner classes are of three types depending on how and where you define them. They are [1] Inner Class
[2] Method-local Inner Class
[3] Anonymous Inner Class
Inner Class :
Inner class, we just need to write a class within a class. Unlike a class, an inner class can be private and once we declare an inner class private, it cannot be accessed from an object outside the class.
Method-local Inner Class :
...
... In Java, we can write a class within a method and this will be a local type. Advanced java training in bangalore Like local variables, the scope of the inner class is restricted within the method.
A method-local inner class can be instantiated only within the method where the inner class is defined.
Anonymous Inner Class :
An inner class declared without a class name is known as an anonymous inner class. In case of anonymous inner classes, we declare and instantiate them at the same time. Generally, they are used whenever we need to override the method of a class or an interface.
Advantage of java inner classes :
They are as follows:
1) Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private. Java training center bangalore
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.
Why Use Nested Classes?
Compelling reasons for using nested classes include the following:
[1] It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.
[2] It increases encapsulation: java course in bangalore Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A , A's members can be declared private and B can access them . In addition, B itself can be hidden from the outside world.
[3] It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.
Difference between nested class and inner class in Java :
Inner class is a part of nested class. Non-static nested classes are known as inner classes. java/j2ee classes bangalore
Internal working of Java member inner class
The java compiler creates two class files in case of inner class. The class file name of inner class is "Outer$Inner". If we want to instantiate inner class, we must have to create the instance of outer class. In such case, instance of inner class is created inside the instance of outer class.
Internal code generated by the compiler
The java compiler creates a class file named Outer$Inner in this case. The Member inner class have the reference of Outer class that is why it can access all the data members of Outer class including private. best java training institute bangalore Marathahalli


This is the short description about the inner class .To get the more knowledge on java join Infocampus Software Training Institute and fet 100% job assistance.

Author : Infocampus is one of the best institute for java training that provides best core java training in bangalore. It provides java training in lesser fees with the live project. Advanced java training is provided by the 10+ years experienced trainers. 4 days free demo classes are available for java/j2ee classes bangalore.
Learn Packages, struts framework , multithreading, singleton class etc in Core & Advanced java class. For providing the best core java training every weekend mock test and mock interviews are conducted.
To book the seat for free demo class call at 9738001024 or to get the syllabus of java course visit at : http://www.infocampus.co.in/java-training-bangalore.html

Total Views: 387Word Count: 840See All articles From Author

Add Comment

Computer Programming Articles

1. Software Business Ideas: Innovate, Create, Dominate
Author: DigitalDazzle

2. The Role Of Erp Systems In Zambia's Business Development
Author: Doris Rose

3. How Useful It Can Be To Do Web Scraping For Flight Data?
Author: #WebScrapingForFlighData

4. Keeping Track Of Time: A Look At Time And Attendance Systems Pen_spark
Author: Computime Systems

5. Self Service Kiosk Machine Hardware And Software Supplier In Dubai
Author: panashi

6. What Are The Benefits Of Erp?
Author: Subho Chowdhry

7. How Can You Learn Excel Online?
Author: Tech Ford

8. Unveiling The Art And Science Of On-demand App Development
Author: CMExpertise Infotech

9. Master Mobile App Development With Swiftic App Framework Certification
Author: EDCHART

10. How On-demand Travel Data Scraping Can Revolutionize The Travel Industry?
Author: #OnDemandTravelDataScraping

11. How To Adopt Mobile First Approach: A Practical Guide For Consumer Retail Brands
Author: Nitin Lahoti

12. Unlock Your Potential With Xamarin Developer Certification
Author: EDCHART

13. Unlock Your Potential With Edchart's Flutter Developer Certification Exam - Free Test
Author: EDCHART

14. Navigating Success: Hiring Dedicated Php Developers For Your Next Project
Author: Quickway Infosystems

15. Cmmc Gap Analysis: Roadmap To Compliance
Author: Karmai

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: