This question already has answers here:
Why are you not able to declare a class as static in Java?
(14 answers)
Closed 9 years ago.
I am trying to create static class in Java Web Application using eclipse. but it says
Illegal modifier for the class ClassName; only public, abstract & final are permitted
can we create static class in web application ? if no why ?
No, because there are no static top-level classes in Java.
Static class you can create in general only as nested class in another class. This has nothing to do with Web-Application. It has a general validity in Java.
Each compilation unit should contain a public or default (non-static) class. Then within it you can declare static nested classes.
Only inner classes can be made as static.
That means except inner class you can not create static class
A static class is, in practice, nothing else than a standard non-inner class declared in another class.
In other words, your app web application has nothing to do with classes being declared as static or not.
Outer class cannot be static. Only nested class can be static. For example below is possible
class OuterClass {
static class StaticNestedClass {
...
}
}
But not
static class OuterClass {
...
}
Related
It's my first time coming across such pattern of code, it might be fairly familiar to advanced gurus out there:
public static class Myclass{
public static class myChildClass{
public int a =1; //this doesn't give an error although I expected it should be declared static
}
}
I can't instantiate the static inner class and I can't access the variable int a outside the class, so does this variable become private to the class? Why does java allow this instead of complaining it be declared static?
I can't instantiate the static inner class
Of course you can:
Myclass.myChildclass m = new Myclass.myChildclass();
and I can't access the variable int a outside the class
Yes you can:
System.out.println(m.a);
so does this variable become private to the class?
No, because it's not private.
Why does java allow this instead of complaining it be declared static?
I think you misunderstand what a static class is: it's merely a nested class without an implicit reference to an instance of the enclosing class that created it. There is no requirement for its members to be static.
This question already has answers here:
Why would a static nested interface be used in Java?
(11 answers)
Closed 4 years ago.
I would think this is easily googleable question, but I found nothing. In the following code:
public class ParentClass {
public static interface InterfaceStatic {
}
public interface InterfaceNotStatic
{
}
}
Can you describe the difference between InterfaceNotStatic and InterfaceStatic? I know what the same syntax means for nested classes, but since interfaces hold no value, the purpose of static here eludes me.
Both declarations are the same. The static modifier is redundant in this case.
A member interface is implicitly static (§9.1.1). It is permitted for the declaration of a member interface to redundantly specify the static modifier.
(quote from JLS 8.5.1. Static Member Type Declarations)
Static inner interface and inner interface is the same, all access rules are the same as with inner static class. So inner interface can be accessible only if you have access to its parent class/interface. In case below you will have access to interface B only from package of interface A, because A has default access modifier. BTW: interface B could be static or not.
This question already has answers here:
Getting hold of the outer class object from the inner class object
(7 answers)
Closed 6 years ago.
I have basic classes
abstract class Unit {
Unit target;
abstract class UnitAI {/*...*/}
}
From these, I have derived
class Infantry extends Unit {
class InfantryAI extends UnitAI {/*...*/}
}
Can the class InfantryAI somehow get the secondary(implicit) this that is used for accessing the members of it's surrounding class Infantry?
Specifically, it needs to determine if its surrounding class Infantry is being targetted by its target, like this:
if (/*secondary_this.*/target.target == secondary_this)
or, generally, by another Unit.
You can access the outer this by prepending the classname:
Infantry.this.target; //"this" of the Infantry class from inside InfantryAI
Unit.this.target; //"this" of the Unit class from inside UnitAI
This doesn't work with static nested classes though as they don't belong to a instance of the outer class.
This question already has answers here:
Java: Static vs inner class [duplicate]
(8 answers)
Closed 9 years ago.
What is the purpose of defining a static class inside a normal class.
public class ClassA extends ClassB implements IA, IB {
public static classStatic extends ClassC implements I1, I2 {
}
}
What is the purpose of defining a static class inside a normal class.
Do all fields/variables need to be static, if its accessing inside the static class.
Can anyone explain/show article me what exactly is achieved in oops by implementing this in JavaScript way.
In Java you can only have one public/package only class per file; that class may require some computation/logic that is to complex and would fit better in another class but it is not general enough to be in another file, it is tight to the encompassing class.
If the inner class wouldn't be static, then you would need an instance of the encompassing class (in your case ClassA) to instantiate the inner class ClassC.
No they do not need to be static; it is a class like any other class from this point of view.
JavaScript is not a class based object-oriented language, so you cannot have classes of any kind.
This question already has answers here:
Static nested class in Java, why?
(14 answers)
Closed 9 years ago.
I have been studying about static inner class in java. But i am not clear whats the point of using static inner class or inner class.
class A{
static class B{
}
public static void main(String[] args) {
B b=new B();
}
}
or
class B{}
class A{
public static void main(String[] args) {
B b=new B();
}
}
Non-static inner classes have an automatic reference to their enclosing class. A static inner classes only relationship to its enclosing class is that you have to reference it via the enclosing class' name: EnclosingClass.StaticInnerClass.
Non-static inner classes are good when you want to reference some of the data from the parent class.
A static inner class is good when you just want to associate the inner class with the enclosing class without dragging it along for the ride.
In other words, a non-static inner class can prevent the enclosing class from being garbage collected, since it has that reference, while a static inner class will never do that.
There is technical difference:
class A {
private static int x = 42; //the answer
public static class B {
int showX() {
return x; // only static class can it
}
}
}
But it isn't the main point. If class B is used only by class A it's good to make it inner because some classes in one package may want to have utility class with same name.
The result is the same, but if B is a little Class that A uses it just makes more sense to put it into A.
By making a nested classes can be static, you can use the nested class without having an instance of the outer class.
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.
...
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.
See tutorial here
When your inner class is considered as part of your "object", use inner class.
Indeed, you would be able to access private,package,protected and public fields from your wrapping class.
"Drawback" is: An inner class can't exist without it's wrapping class instantiated, that is logically due to the first sentence.
Otherwise, if you consider:
The behaviour of your nested(called also static inner) class isn't considered as reusable by external class since maybe too specific.
some related fields of one class as being so much related that you want to make
a class wrapping them. This will get your code more understandable and cleaner.
then choose to make a static class.
Moreover, since static (meaning outside the life cycle of any object), a nested class may instantiate without regarding its wrapping class/object.