- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between static classes and non-static inner classes in Java?
Following are the notable differences between inner classes and static inner classes.
Accessing the members of the outer class
The static inner class can access the static members of the outer class directly. But, to access the instance members of the outer class you need to instantiate the outer class.
Example
public class Outer { int num = 234; static int data = 300; public static class Inner{ public static void main(String args[]){ Outer obj = new Outer(); System.out.println(obj.num); System.out.println(data); } } }
Output
234 300
The non inner class can access the members of its outer class (both instance and static) directly without instantiation.
Example
public class Outer2 { int num = 234; static int data =300; public class Inner{ public void main(){ System.out.println(num); System.out.println(data); } } public static void main(String args[]){ new Outer2().new Inner().main(); } }
Output
234 300
Having static members in inner class
You cannot have members of a non-static inner class static. Static methods are allowed only in top-level classes and static inner classes.
- Related Articles
- Static Nested Classes in Java
- Static methods in JavaScript classes?
- What is the difference between non-static methods and abstract methods in Java?
- What are inner classes in Java?
- What are anonymous inner classes in Java?
- What is the difference between interfaces and abstract classes in Java?
- Static and non static blank final variables in Java
- What are method local inner classes in Java?
- What are the differences between a static and a non-static class in C#?
- Difference Between Static and Final in Java
- What is the difference between objects and classes in C#?
- What is the difference between static and dynamic polymorphism?
- What is the difference between String, StringBuffer and StringBuilder classes in Java explain briefly?
- Non static blocks in Java.
- Why do we need inner classes in Java?

Advertisements