I have found code in which static class contains methods that are not declared static. Compiler doesn't display any warning. It seems confusing, doesn't it?
6 Answers
There is no concept of static class in java (not even static inner class). If you see class is static and it is in working condition then it must be inner class(also called as nested class) which is declared as static. And there is no restriction to have only static methods in static inner classes.
The significance of declaring an inner class static is to be able to create instances of the nested class independent of the class in which it is nested. If static is not mentioned then every instance of the nested class will be associated with an instance of the class in which it is nested.
This question has more details. Java inner class and static nested class

- 1
- 1

- 16,024
- 8
- 58
- 85
-
3
-
@MathiasSchwarz: not really. At least not the way it's often seen. C# has "static classes" and what you see here is something very different. – Joachim Sauer Sep 11 '12 at 08:33
-
-
1Sure, words can mean something else in other languages, but this question is about Java. In Java a static class is an inner class without a pointer to the outer class/object. – Mathias Schwarz Sep 11 '12 at 08:34
-
@MathiasSchwarz: but since the question implies an understanding that seems to come from C#, the fact that it means something else in C# **is** relevant to this question. Context is important. – Joachim Sauer Sep 11 '12 at 08:41
-
@MathiasSchwarz : You took very straight forward meaning, but my answer also says that you didn't understand : `there is no static class concept other than static inner classes.` – Nandkumar Tekale Sep 11 '12 at 09:18
-
There is no such thing as a 'static inner class'. See [JLS #8.1.3](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.3): 'An inner class is a nested class that is not explicitly or implicitly declared static'. An 'inner class' is by definition non-static. A 'nested class' may be static or non-static. – user207421 Sep 11 '12 at 09:58
-
-
..so we can say- "as we declare class as static in Java, C# or C++; there is the concept of static class in all these languages. And its meaning may vary from one language to other". – Dexter May 08 '15 at 05:42
-
'If you see class is static and it is in working condition then it must be inner class' - this isn't correct. An Inner class is always non-static. A nested class declared static is called a Static Nested Class. Both, are nested classes (which is a general term used to describe classes within classes). – Zippy Jul 16 '18 at 13:39
The answers here are good, but I guess I should add an example. Consider the following class definition
class Foo{
class Bar{
int x;
}
}
To use the class Bar you'll have to create an instance of class Foo first.
Foo foo = new Foo();
Foo.Bar bar = foo.new Bar();
bar.x = 10;
But if you have used the static keyword like the following
class Foo{
static class Bar{
int x;
}
}
You could have skipped a step
Foo.Bar bar = new Foo.Bar();
bar.x = 10;
If you'll try to statically use Foo.Bar directly when the inner class is not static, then you'll get the following error -
error: an enclosing instance that contains Foo.Bar is required
Foo.Bar bar = new Foo.Bar();
^
1 error
I think this example should clear it.

- 19,522
- 20
- 117
- 184
A static class is a nested class which has no implicit reference to the outer class. A static class can have static methods or instance methods. Note: an inner class can't have static methods.

- 525,659
- 79
- 751
- 1,130
-
2From JLS 8.1.3 "An inner class is a nested class that is not explicitly or implicitly declared static. " http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html – Peter Lawrey Sep 11 '12 at 08:36
-
@PeterLawrey "A static class is a nested class which has no implicit reference to the outer class" - this is the most important aspect of all in my opinion. I wish I could upvote this twice. – Eugene Sep 11 '12 at 08:56
-
Note: local and anonymous classes are also inner classes even if they are defined in a static content. i.e. with no implied outer instance. – Peter Lawrey Sep 11 '12 at 09:07
There's no such thing as a "static class" in Java (at least not in the way you seem to understand it). In C# there is a "static class" that can not be instantiated and can't have non-static members. That concept doesn't exist explicitly in Java (you can get the same effect by making all constructors private
, however).
What you're seeing is probably a static nested class like this:
class Foo {
static class Bar {
}
}
This only means that a Bar
instance does not have a reference to an outer Foo
instance (as it would be the case if Bar
did not have the static
modifier).
Bar
can have any kind of members it could have if it were a top-level class.

- 302,674
- 57
- 556
- 614
The static
keyword is totally different for attributes/methods and classes.
For attributes and methods, it will tell the compiler to have only one instance of the attribute or method that can be referenced without a living instance of the class.
The static class
can only be used for inner class, to indicate that the class can be referenced without an existing instance of the enclosing class. It is therefore useful for inner enumeration that should be accessed from outside.
But a static class
can reference whatever it wants, from its own body.

- 2,636
- 1
- 21
- 26
-
No. See [JLS #8.1.3](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.3). Your explanation of static methods is also incorrect. – user207421 Sep 11 '12 at 10:02
-
@EJP I believe the above post was a direct address to the question at hand, to clarify the OP's misunderstanding of the static reference used in the context of a class (inner) & as referenced to a member field. So I believe, informally, in a broad sense, he might be right. Or please clarify my understanding. – sharhp Aug 10 '17 at 14:32
-
Only inner classes can be declared static
. A static class has no pointer to its outer class and can therefore only refer to static fields and methods of the outer class. A static class may however itself contain non-static methods.

- 7,099
- 23
- 28
-
An 'inner class' is by definition non-static. You mean 'nested class'. – user207421 Sep 11 '12 at 09:57