When would I want to make my private class static? - java

In general, are there any benefits in declaring a private class as static?
In what cases would I want to use one of the following over the other?
private static class Foo
{
...
}
vs
private class Foo
{
...
}

I think this is a good starting point:
http://java67.blogspot.fi/2012/10/nested-class-java-static-vs-non-static-inner.html
1) Nested static class doesn't need reference of Outer class but non
static nested class or Inner class requires Outer class reference. You
can not create instance of Inner class without creating instance of
Outer class. This is by far most important thing to consider while
making a nested class static or non static.
2) static class is actually static member of class and can be used in
static context e.g. static method or static block of Outer class.
3) Another difference between static and non static nested class is
that you can not access non static members e.g. method and field into
nested static class directly. If you do you will get error like "non
static member can not be used in static context". While Inner class
can access both static and non static member of Outer class.

If i understand correctly, the question is for private class vs private static class. All the responses are generally about inner classes, that are not 100% applied to that question. So first things first:
From geeksforgeeks:
Nested class -> a class within another class
static nested class -> Nested classes that are declared static are called static nested classes
inner class -> An inner class is a non-static nested class.
As the accepted response says, static vs non-static nested classes differ on the way and possibility to access methods/fields outside the outer class. But in case of private classes B within class A, you dont have this issue, cause B is not accessible outside A anyway.
Now, from inside class A, for non-static fields/methods you can always refer to class B, either by saying new A.B() or just new B() and it doesnt matter (no compilation/runtime errors) if B is private class or private static class. In case of static fields/methods you need to use a private static class.
Moreover, if you want to access from inside B a non-static field of A, then you can't have B as private static class.
I generally prefer private static class, except when i cant use it like in the previous case, cause intellij will give warnings otherwise.

If you need access to the member variables/methods of the enclosing class, use the non-static form. If you don't, use the static form.

I would assume you are referring to inner classes.
I think the motivation would be coming from how you want to associate your inner class. If you want your inner class to be associated to a specific instance of its outer class, you'd use private class, otherwise, use private static class.

I found it useful in having a specific exception in a generic abstract class. I.e.:
public abstract class AbstractClass <T>
{
private void doSomethingOrThrowException() throws SpecificException
{
....
if ( ! successful)
{
throw new SpecificException();
}
}
private static class SpecificException extends Exception {}
}
If I were to leave out the static, the compiler would give me an error that states: The generic class AbstractClass<T>.SpecificException may not subclass java.lang.Throwable

static classes differ from ordinary classes only in that they can be accessed without their instances being created. so if you need some class to be accessable every time, use static

Related

Why outer class can not be declared static? [duplicate]

I am trying to find why the class cant be created as a static? Like:
public static class Qwert{
public static void main(String args[]){
int x = 12;
while(x<12){
x--;
}
System.out.println(" the X value is : "+ x);
}
}
In Java, the static keyword typically flags a method or field as existing not once per instance of a class, but once ever. A class exists once anyway so in effect, all classes are "static" in this way and all objects are instances of classes.
static does have a meaning for inner classes, which is entirely different: Usually an inner class instance can access the members of an outer class instance that it's tied to, but if the inner class is static, it does not have such a reference and can be instantiated without an instance of the outer class. Maybe you saw that someplace, then tried to use it on a top-level class, where it isn't meaningful.
Or maybe you saw it in other languages like C#, whose syntax is an awful lot like Java's.
(One time I couldn't figure out why an outer class instance wasn't being garbage-collected -- it was because I was keeping a reference to one of its inner class instances elsewhere, and the inner class was not static and so had a reference to the outer class instance. So by default, I make inner classes static now.)
To prevent a particular class being instantiated you should add a private Constructor. This stops 'any other' Class from being able to create an object of type Qwert.
for example:
public static class Qwert{
private Qwert() {}
public static void main(String args[]){
int x = 12;
while(x<12){
x--;
}
System.out.println(" the X value is : "+ x);
}
}
We should define members as static which
Should be common to all objects of the class.
Should belong to the class and accessible by class name.
Should not need an object of class to access them.
Now suppose we are defining an outer class as static and suppose we are allowed to do so. Will this serve any purpose or provide any advantage to a developer or it will create ambiguity and complications for both developers and language creators?
Let’s check, defining an outer class as static will serve purposes which we have defined above or not?
Every class is already common to all of its objects and there is no need to make it static to become available to all of its objects.
We need a class name to access its static members because these members are part of class while an outer class is part of package and we can directly access the class by just writing package_name.class_name (similar to class_name.static_field_name), So again there is no need to do which is already there by default.
We do not need any object to access a class if it is visible, we can simply write package_name.class_name to access it. And by definition, a class is a blueprint for its objects and we create a class to create objects from it (exception will always be there e.g. java.lang.Math), again there is no need to define an outer class as static.
From above points, we can say Java creators had not allowed an outer class to be static because there is no need to make it static. Allowing to make the outer class static will only increase complications, ambiguity and duplicity. Read more on Why An Outer Java Class Can’t Be Static
To prevent any class from creating an instance of Qwert, either by inheritance or by using reflection, you make the constructor fail by placing a poison pill:
public class Qwert {
private Qwert() throws IllegalAccessException {
throw new IllegalAccessException("Utility class!");
}
public static class Yuiop {
public Yuiop() throws IllegalAccessException {
// generates a synthetic accessor method to super()
}
}
public static void main(String args[]) {
new Yuiop();
}
}
its because when we use static keyword for a component, that component becomes a class level component and its memory is taken by its class.
In Java, by definition, static applies to the inner components of a class. "X is static" means in Java "X is associated with the class in which it is defined, rather than with any instance of the class".
The word "static" means literally "fixed at one location in memory". Every instance of the class shares a static variable or static member. Hence the use of "class variable" as a synonym for "static variable". Which lets you see at once that you cannot define an outer class as static.
It therefore follows that your class, Qwert, cannot be created as static. Unless it is subsumed as a component of an outer class, effectively making it an inner class.

private abstract classes in Java [duplicate]

This question already has answers here:
Why nested abstract class in java
(2 answers)
Closed 8 years ago.
I am a relative newcomer to Java. I recently came across a private static abstract class inside a regular class while browsing some Android app source code. What could be a use case for such a nested class? How would it be used and what sort of design benefits are there from using such a class?
I've never come across this pattern before myself, but I can imagine it being useful if:
You want to implement an interface in a similar way in a bunch of nested classes (e.g. to be returned from public methods within the enclosing class)
Those interface implementations have a lot of code in common (hence the abstract class)
You don't need any code other than the implementations to know about the abstract class
The subclasses of the abstract class may well be private as well. (Typically when I write nested classes, they're private implementation details.) For example:
public interface Foo {
// Methods here
}
public class FooFactory {
public static Foo getFoo1() {
return new Foo1();
}
public static Foo getFoo2() {
return new Foo2();
}
private static abstract class AbstractFoo implements Foo {
// Implement methods in Foo in terms of
// doSomething()...
// Implementation-specific method
public abstract void doSomething();
}
private static class Foo1 extends AbstractFoo {
public void doSomething() {
}
}
private static class Foo2 extends AbstractFoo {
public void doSomething() {
}
}
}
What could be a use case for such a nested class?
You would use this if:
you were going to implement a number of nested classes with common functionality, and
you didn't want the base class with that functionality to be visible.
You would probably also make the leaf classes either final or private.
How would it be used and what sort of design benefits are there from using such a class?
See above. Basically, you are hiding the class so that it cannot be directly subclassed outside of the outermost enclosing class. I think this will also prevent the subclasses from being used polymorphically outside of the outermost enclosing class.
This is not a common use-case, but I imagine it is sensible in the context that you found it.
A typical use is to replace the equivalent of a C struct - for instance a small class that contains a name and a value and gets stored in a List or Map in the enclosing class. Because it is not used outside of the compilation unit, it can be private.
The static keyword usage is a bit odd here; all it means is that the class has no connection with the enclosing class.
Making it abstract is unusual - it indicates that there will be concrete implementations and I have never done that. YMMV...
The purpose of a nested class is to clearly group the nested class with its surrounding class, signaling that these two classes are to be used together.
Nested classes are considered members of their enclosing class. Thus, a nested class can be declared public, package (no access modifier), protected and private (see access modifiers for more info).
Static Nested Classes
Static nested classes are declared like this:
public class Outer {
public static class Nested {
}
}
In order to create an instance of Nested you must reference it by prefixing it with the Outer class name, like this:
Outer.Nested instance = new Outer.Nested();
A static nested class is essentially a normal class that has just been nested inside another class. It interacts with its enclosing class in the same way. Being static, a static nested class can only access instance variables of the enclosing class via a reference to an instance of the enclosing class.

Static classes and final classes in java [duplicate]

This question already has answers here:
What is the point of "final class" in Java?
(24 answers)
Closed 9 years ago.
In Java (and in Android), what's the use of static class and final class declarations?
My question is not about static instances but class declarations like,
static class StaticClass {
//variables and methods
}
and
final class FinalClass {
//variables and methods
}
Thanks,
Static Nested Classes
As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.
Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
Static nested classes are accessed using the enclosing class name:
OuterClass.StaticNestedClass
For example, to create an object for the static nested class, use this syntax:
OuterClass.StaticNestedClass nestedObject =
new OuterClass.StaticNestedClass();
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Final Classes
A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String class.
http://docs.oracle.com/javase/tutorial/java/IandI/final.html
final classes will restrict for further extends (Inherit).
You can not use static keyword on outer class,static is permitted only to inner classes Static classes
You make a class final so it can not be extended. Usually it makes sense if you'are creating a library (or working on a part of a big project), so your clients are not able to extend the class and modify the existing behavior. In your own program there's little reason to make a class final unless it's a big program and you can inadvertently forget things.
Static inner classes are for things that logically belongs to an outer (containing) class but which have no dependencies on the state of the outer class. For example you can have a Parser class and an inner Parser.Listener class. Normally if you decide to have an inner class try, first, to make it static, if possible, to simplify things.
You could do without both final and static inner classes then with experience you will find use for them.
other class can not extends the final class exmple String is final class so you cannot extends this class.
You cannot have a 'top-level' class declared static. You can only have an inner class with the modifier 'static'.
static inner class only access the static member of the outer class
if you make a class as final then it can not be inherited. Generally top level class can not be made static however inner class can be made as static and Nested static class doesn’t need reference of Outer class
static and final class in java
you can't mark a class static ?
yes, if it is nested class
no, if it is normal class.
if you mark a nested class static it will work as fully flagged class just use a classname.staticClassNmae to access it.
if you mark a class final , it can't be inherited , a good example of this String class in java.

Static class Initialisation in Java

Is there a difference between initialisation of a static nested class and top level class?I understand that static class doesnt require an enclosing class instance but what happens if there are multiple instances of the static nested class? Just like static variables are shared by class instances, will the instances of the static class be shared also?
Let me see if I understand your question correctly.
A class can declare nested classes.
If a class C1 declares a non-static inner class C2, then C2 has access to all of C1's fields and methods, regardless of their access modifiers.
C2 is, in fact, treated as a field: its declaration is loaded whenever a new instance of C1 is created. This means that non-static inner classes are rather more expensive than static ones, and should be avoided if not strictly necessary.
If a class C1 declares a static inner class C3, then C3 is shared across all instances of C1. It has access to all static methods and fields of C1, but not to non static ones - C3 is by definition not tied to a specific instance of C1, so there is nothing for it to have access to.
When you declare a static inner class, you're not saying anything about its instances. You're just telling the compiler that the class' definition is shared across all instances of the enclosing class. So, no, instances of the nested static class aren't shared automatically.
A static nested class does not required an instance of the enclosing class (as you point out) so there is nothing to share.
If you have a static variable then every instance of the class will hold a reference to the same static variable. Changes in one class will change the variable in all classes.
As a class is immutable at run time then this same logic doesn't carry through.
An instance of a static nested class is effectively the same as an instance of any other class.
The only way an instance would be shared would be if you had a static variable pointing to an instance of a static nested class. In this case it is the same as any other static variable.
As is pointed out the the tutorial the only real different between a static nested class and a top level class is that a static nested class can access private static members of it's enclosing class.
Every class is a singleton object of type Class.
A static inner class is the base case. It is the other, normal inner class, that also has an OuterClass.this pointer for its instance objects.
So as such, there is no difference in class initialisation of any class.
The nested class as you declared it as "static class" does not differ from another top level class like inner class does. Adding the static to it declaration you promote it to be separated from owner class that became only a namespace for it.
package org.stack.question
public class Top {
public static class Nested {
}
}
To create a instance of Nested class you must only do this
Object instance = new org.stack.question.Top.Nested();
From specification:
Nested classes that are not inner classes may declare static members
freely, in accordance with the usual rules of the Java programming
language. Member interfaces (§8.5) are implicitly static so they are
never considered to be inner classes.

Why can't we have static method in a (non-static) inner class (pre-Java 16)?

Why can't we have static method in a non-static inner class?
public class Foo {
class Bar {
static void method() {} // Compiler error
}
}
If I make the inner class static it works. Why?
public class Foo {
static class Bar { // now static
static void method() {}
}
}
In Java 16+, both of these are valid.
Because an instance of an inner class is implicitly associated with an instance of its outer class, it cannot define any static methods itself. Since a static nested class cannot refer directly to instance variables or methods defined in its enclosing class, it can use them only through an object reference, it's safe to declare static methods in a static nested class.
There's not much point to allowing a static method in a non-static inner class; how would you access it? You cannot access (at least initially) a non-static inner class instance without going through an outer class instance. There is no purely static way to create a non-static inner class.
For an outer class Outer, you can access a static method test() like this:
Outer.test();
For a static inner class Inner, you can access its static method innerTest() like this:
Outer.Inner.innerTest();
However, if Inner is not static, there is now no purely static way to reference the method innertest. Non-static inner classes are tied to a specific instance of their outer class. A function is different from a constant, in that a reference to Outer.Inner.CONSTANT is guaranteed to be unambiguous in a way that a function call Outer.Inner.staticFunction(); is not. Let's say you have Inner.staticFunction() that calls getState(), which is defined in Outer. If you try to invoke that static function, you now have an ambiguous reference to the Inner class. That is, on which instance of the inner class do you invoke the static function? It matters. See, there is no truly static way to reference that static method, due to the implicit reference to the outer object.
Paul Bellora is correct that the language designers could have allowed this. They would then have to carefully disallow any access to the implicit reference to the outer class in static methods of the non-static inner class. At this point, what is the value to this being an inner class if you cannot reference the outer class, except statically? And if static access is fine, then why not declare the whole inner class static? If you simply make the inner class itself static, then you have no implicit reference to the outer class, and you no longer have this ambiguity.
If you actually need static methods on a non-static inner class, then you probably need to rethink your design.
I have a theory, which may or may not be correct.
First, you should know some things about how inner classes are implemented in Java. Suppose you've got this class:
class Outer {
private int foo = 0;
class Inner implements Runnable {
public void run(){ foo++; }
}
public Runnable newFooIncrementer(){ return new Inner(); }
}
When you compile it, the generated bytecode will look as if you wrote something like this:
class Outer {
private int foo = 0;
static class Inner implements Runnable {
private final Outer this$0;
public Inner(Outer outer){
this$0 = outer;
}
public void run(){ this$0.foo++; }
}
public Runnable newFooIncrementer(){ return new Inner(this); }
}
Now, if we did allow static methods in non-static inner classes, you might want to do something like this.
class Outer {
private int foo = 0;
class Inner {
public static void incrFoo(){ foo++; }
}
}
... which looks fairly reasonable, as the Inner class seems to have one incarnation per Outer instance. But as we saw above, the non-static inner classes really are just syntactic sugar for static "inner" classes, so the last example would be approximately equivalent to:
class Outer {
private int foo = 0;
static class Inner {
private final Outer this$0;
public Inner(Outer outer){
this$0 = outer;
}
public static void incrFoo(){ this$0.foo++; }
}
}
... which clearly won't work, since this$0 is non-static. This sort of explains why static methods aren't allowed (although you could make the argument that you could allow static methods as long as they didn't reference the enclosing object), and why you can't have non-final static fields (it would be counter-intuitive if instances of non-static inner classes from different objects shared "static state"). It also explains why final fields are allowed (as long as they don't reference the enclosing object).
The only reason is "not a must", so why bother to support it?
Syntactically,there is no reason to prohibit an inner class from having static members. Although an instance of Inner is associated with an instance of Outer, it's still possible to use Outer.Inner.myStatic to refer a static member of Inner if java decides to do so.
If you need to share something among all the instances of Inner, you can just put them into Outer as static members. This is not worse than you use static members in Inner, where Outer can still access any private member of Inner anyway(does not improve encapsulation).
If you need to share something among all the instances of Inner created by one outer object,it makes more sense to put them into Outer class as ordinary members.
I don't agree the opinion that "a static nested class is pretty much just a top level class". I think its better to really regard a static nested class/inner class as a part of the outer class, because they can access outer class's private members. And members of outer class are "members of inner class" as well. So there is no need to support static member in inner class. An ordinary/static member in outer class will suffice.
From: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.
Oracle's explanation is superficial and handwavy. Since there's no technical or syntactic reason to preempt static members within an inner class (it's allowed in other languages such as C#) the Java designers' motivation was likely conceptual taste and/or a matter of technical convenience.
Here's my speculation:
Unlike top-level classes, inner classes are instance-dependent: an inner-class instance is associated with an instance of every one of its outer classes and has direct access to their members. This is the chief motivation for having them in Java. Expressed another way: an inner class is meant for instantiation in the context of an outer class instance. Without an outer class instance, an inner class ought not be any more usable than the other instance members of the outer class. Let's refer to this as the instance-dependent spirit of inner classes.
The very nature of static members (which are NOT object-oriented) clashes with the instance-dependent spirit of inner classes (which IS object-oriented) because you can reference/call a static member of an inner class without an outer class instance by using the qualified inner class name.
Static variables in particular may offend in yet another way: two instances of an inner class that are associated with different instances of the outer class would share static variables. Since variables are a component of state, the two inner class instances would, in effect, share state independently of the outer class instances they're associated with. It’s not that it’s unacceptable that static variables work this way (we accept them in Java as a sensible compromise to OOP purity), but there’s arguably a deeper offense to be had by allowing them in inner classes whose instances are already coupled with outer class instances by design. Forbidding static members within inner classes in favor of the instance-dependent spirit reaps the added bonus of preempting this deeper OOP offense.
On the other hand, no such offense is entailed by static constants, which do not meaningfully constitute state and so these are allowable. Why not forbid static constants for maximum consistency with the instance-dependent spirit? Perhaps because constants need not take up more memory than necessary (if they're forced to be non-static then they’re copied into every inner class instance which is potentially wasteful). Otherwise I can’t imagine the reason for the exception.
It may not be rock-solid reasoning but IMO it makes the most sense of Oracle's cursory remark on the matter.
Short answer: The mental model most programmers have of how scope works is not the model used by javac. Matching the more intuitive model would have required a big change to how javac works.
The main reason that static members in inner classes are desirable is for code cleanliness - a static member used only by an inner class ought to live inside it, rather than having to be placed in the outer class. Consider:
class Outer {
int outID;
class Inner {
static int nextID;
int id = nextID++;
String getID() {
return outID + ":" + id;
}
}
}
Consider what is going on in getID() when I use the unqualified identifier "outID". The scope in which this identifier appears looks something like:
Outer -> Inner -> getID()
Here, again because this is just how javac works, the "Outer" level of the scope includes both static and instance members of Outer. This is confusing because we are usually told to think of the static part of a class as another level of the scope:
Outer static -> Outer instance -> instanceMethod()
\----> staticMethod()
In this way of thinking about it, of course staticMethod() can only see static members of Outer. But if that were how javac works, then referencing an instance variable in a static method would result in a "name cannot be resolved" error. What really happens is that the name is found in scope, but then an extra level of check kicks in and figures out that the name was declared in an instance context and is being referenced from a static context.
OK, how does this relate to inner classes? Naively, we think there is no reason inner classes can't have a static scope, because we are picturing the scope working like this:
Outer static -> Outer instance -> Inner instance -> getID()
\------ Inner static ------^
In other words, static declarations in the inner class and instance declarations in the outer class are both in scope within the instance context of the inner class, but neither of these is actually nested in the other; both are instead nested in the static scope of Outer.
That's just not how javac works - there is a single level of scope for both static and instance members, and scope always strictly nests. Even inheritance is implemented by copying declarations into the subclass rather than branching and searching the superclass scope.
To support static members of inner classes javac would have to either split static and instance scopes and support branching and rejoining scope hierarchies, or it would have to extend its simple boolean "static context" idea to change to track the type of context at all levels of nested class in the current scope.
Why can't we have static method in a non-static inner class ?
Note: A non-static nested class is known as inner class so you do not have non-static inner class as such.
An inner class instance has no existence without a corresponding instance of outer class. An inner class cannot declare static members other than compile time constants. If it were allowed then there would have been ambiguity about meaning of static. In that case there would have been certain confusions:
Does it mean there is only one instance in VM?
Or only one instance per outer object?
That is why the designers probably took the decision of not handling this issue at all.
If I make the inner class static it works. Why ?
Again you cannot make an inner class static rather you can declare a static class as nested. In that case this nested class is actually part of outer class and can have static members without any issue.
This topic has garnered attention from many, still I will try to explain in the most simplest of terms.
Firstly, with reference to http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.1, a class or interface is initialized immediately before the first occurence/invocation of any member which is preceeded by the static keyword.
So, if we put up with a static member within an inner class, it will lead to the initialization of the inner class, not necessarily the outer/enclosing class. So, we hamper the class initialization sequence.
Also consider the fact that a non-static inner class is associated with the instance of an enclosing/outer class. So, associating with an instance will mean, that the inner class will exist inside an Outer class instance and will be different amongst instances.
Simplifying the point, in order to access the static member we need an instance of an Outer class, from which we will again need to create an instance of non-static inner class. Static members are not supposed to be bound to instances and therefore you receive a compilation error.
The work to add records to JDK16 also mentions that static methods and fields can now be used with inner classes, even permitting main() to launch the class.
For example this compiles and runs in JDK16, and can select either main() to be run as java Outer or java Outer$Inner:
public class Outer {
public static void main(String[] args) {
System.out.println("Outer class main xxx="+Inner.xxx+" nnn="+(++Inner.nnn)+" iii="+(--iii));
aaa();
Inner.zzz();
}
public static void aaa() {
System.out.println("aaa() nnn="+(++Inner.nnn)+" iii="+(--iii));
}
public static int iii = 100;
class Inner {
public static final String xxx= "yyy";
public static int nnn = 0;
public static void zzz() {
System.out.println("zzz() "+" nnn="+(++nnn)+" iii="+(--iii));
}
public static void main(String[] args) {
System.out.println("Inner class main xxx="+xxx+" nnn="+(++nnn)+" iii="+(--iii));
zzz();
aaa();
}
}
}
An inner class is something completely different from a static nested class although both are similar in syntax. Static nested classes are only a means for grouping whereas inner classes have a strong association - and access to all values of - their outer class. You should be sure why you want to use an inner class and then it should come pretty natural which one you have to use. If you need to declare a static method it's probably a static nested class you want anyway.
First of all why someone want to define the static member in a non-static inner class? answer is, so that the outer class member can use those static member with the inner class name only, Right?
But for this case we can directly define the member in outer class. which will be associated with all object of inner class, within the outer class instance.
like below code,
public class Outer {
class Inner {
public static void method() {
}
}
}
can be written like this
public class Outer {
void method() {
}
class Inner {
}
}
So in my opinion not to complicate the code java designer is not allowing this functionality or we may see this functionality in future releases with some more features.
suppose there are two instances of outer class & they both have instantiated inner class.Now if inner class has one static member then it will keep only one copy of that member in heap area.In this case both objects of outer class will refer to this single copy & they can alter it together.This can cause "Dirty read" situation hence to prevent this Java has applied this restriction.Another strong point to support this argument is that java allows final static members here, those whose values can't be changed from either of outer class object.
Please do let me if i am wrong.
Try to treat the class as a normal field, then you will understand.
//something must be static. Suppose something is an inner class, then it has static keyword which means it's a static class
Outer.something
You are allowed static methods on static nested classes. For example
public class Outer {
public static class Inner {
public static void method() {
}
}
}
It is useless to have inner class members as static because you won't be able to access them in the first place.
Think about this, to access a static member you use className.memberName ,, in our case , it should be something like outerclassName.innerclassName.memberName,,, now do you see why innerclass must be static....

Categories

Resources