Class Declarations in Java
What are the different types of class declarations in Java?
Today I stumbled upon a very old question many interviewers often ask during Java based interviews :
What is the difference between an inner class and a static member class?
Well, I keep forgetting the difference due to lack of usage in day-to-day programming. I shall try to sum up the variety Java offers for class declaration below, evidently answering to the question above.
Get Sumit Mundra’s stories in your inbox
Join Medium for free to get updates from this writer.
Okay let’s start out with all possibilities of classes as specified in JLS ( Java Language Specification ):
- Top level class: This is any ordinary class declaration to write a program in Java or declare a bean. This is either part of a default package
class Example {}
or declared within a package by package declaration at the top of any Java program, ex.package com.package.name
class Example {
...
}
This class can be referred and/or used in any other Java program within same package. If it is declaredpublic, then it can be referred from other packages too, ex.public class Example {
} - Nested class : Any class which is declared within body of another class or declaration. This way, there can be a class within an interface/class (member class) , method (local class) or a code expression (anonymous class).
A) Member class : A nested class which is declared within an interface/class (member class). These can be of two types:
1. Static : withstatickeyword in declaration. These classes can not refer to the instance of surrounding class declaration. Also, on the basis of access modifiers in declaration, these classes objects can be instantiated from other classes and packages directly asA.B. Also, objects of such classes can only access static members of enclosing class and no access to the instance objects.
2. Non-static member class : All non static member classes belong to the category inner classes. They do not havestatickeyword in declaration. These classes are generally used to declare classes to be used in context of objects of enclosing class. Also, there is a limited static declaration possible within non static member classes body. It can be only astatic finaldeclaration for a compile time constant.class Outer{
class Nested{
}
}
B) Local class: As JLS defines, a local class is a nested class that is not a member of any class and that has a name. This would be always contained by a block. No access modifier (public,protectedorprivate) can be used with declaration as well as the keywordstatic. The statements in method can be positioned anywhere in relation to class declaration. ex.void foo() {
class Local {
...
}
}
C) Anonymous class: This is a class mostly declared as an in-situ implementation of an interface. This is also an inner class. It is neverstaticorabstract, alwaysfinaland has no explicit constructor declaration. The compiler creates an anonymous constructor for it. ex.// Anonymous is a abstract class or an interfaceAnonymous a = new Anonymous() {// members and methodspublic void methodB(){…}}
An interesting note : static modifier can only be used with member classes and not with top level classes ( They can be referred from other classes directly via import) or local and anonymous classes.







