What does static in front of nested interface mean? [duplicate] - java

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.

Related

Is it unnecessary to specify public or public abstract in the code below? [duplicate]

This question already has answers here:
Java abstract interface
(9 answers)
Should methods in a Java interface be declared with or without a public access modifier?
(12 answers)
Closed 2 years ago.
interface Airplane {
String fuelOption = "kerosene";
public abstract String getFuelOption();
}
Is it necessary to specify public or public abstract in the line with the getFuelOption() method?
Thank you!
The public access modifier is not needed because
Every method declaration in the body of an interface is implicitly
public (§6.6). It is permitted, but discouraged as a matter of style,
to redundantly specify the public modifier for a method declaration in
an interface. (Section 9.4)
The abstract access modifier is not needed because
A default method is a method that is declared in an interface with the
default modifier; its body is always represented by a block.
And...
An interface method lacking a default modifier or a static modifier is implicitly abstract, so its body is represented by a semicolon, not a block.
Given that default methods have a body, and those that don't are inherently abstract, and every method declaration on an interface is inherently public, you don't need to specify either keyword.
Please refer below JLS :
https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#:~:text=Every%20method%20declaration%20in%20the,method%20declaration%20in%20an%20interface.&text=Default%20methods%20are%20distinct%20from,which%20are%20declared%20in%20classes.
No, All interface methods are public and abstract by default.
relevant text from the Java language spec, Section 9.4
A method in the body of an interface may be declared public or private .If no access modifier is given, the method is implicitly
public.
An interface method lacking a private, default, or static modifier is
implicitly abstract. Its body is represented by a semicolon, not a
block.

public abstract static method - why not allowed? [duplicate]

This question already has answers here:
Why can't static methods be abstract in Java?
(29 answers)
Closed 3 years ago.
I want to have the family of classes which all have the same method. As this method is quite simple I want to make it final static, something like
class A {
public static final String createSth () { }
}
(in fact this method returns only String).
Sometimes it would be useful to upper-cast all those A, B, C classes with the same method. Thus I would like to create mother class.
class abstract Mother {
public static abstract String createSth () { }
}
and then to add the appropriate extends for all my classes A, B, C ex. class A extends Mother.
Unfortunately it's not allowed by Java. I'm wondering why?
Sure I can remove static final modifier and then everything is ok. But on another hand if each subclass returns the constant String which is not modifiable in any way, why not?
You can ask what is the purpose of such a construction?
Simply I want to create database sql strings. For each table I created a separate class, and as a result of createSth method I want to return sql creating a table for this class.
Interfaces does help neither.
The only solutions seems to be to remove abstract modifier from createSth method in Mother class. But then I'm not allowed to uppercast to Mother class and to call createSth method for children. So I have to remove static modifier for children classes.
Concluding why abstract method is not allowed to be static? Ok here I can even understand that, but either why non static abstract method is not allowed to be replaced by static method in child class?
static methods (just like final and private) can't be overriden so there is no way to implement them in derived class so making them abstract would not make sense.
Static methods don't get inherited, so making them abstract would be meaningless.
final methods do not get overridden
static methods do not get overridden, but they can be hidden in the subClass.

can we create static class in Java web application? [duplicate]

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 {
...
}

Making the inner class of a generic class extend Throwable [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why doesn’t Java allow generic subclasses of Throwable?
I'm trying to make a regular RuntimeException inside a generic class like this:
public class SomeGenericClass<SomeType> {
public class SomeInternalException extends RuntimeException {
[...]
}
[...]
}
This piece of code gives me an error on the word RuntimeException saying The generic class SomeGenericClass<SomeType>.SomeInternalException may not subclass java.lang.Throwable.
What has this RuntimeException to do with my class being generic?
Java doesn't allow generic subclasses of Throwable. And, a nonstatic inner class is effectively parameterized by the type parameters of its outerclass (See Oracle JDK Bug 5086027). For instance, in your example, instances of your innerclass have types of form SomeGenericClass<T>.SomeInternalException. So, Java doesn't allow the static inner class of a generic class to extend Throwable.
A workaround would be to make SomeInternalException a static inner class. This is because if the innerclass is static its type won't be generic, i.e., SomeGenericClass.SomeInternalException.
public class SomeGenericClass<SomeType> {
public static class SomeInternalException extends RuntimeException {
[...]
}
[...]
}

What is a static interface in java?

I was reading through the Map.Entry interface, when I noticed it is a static interface. I didn't quite understand what a static interface is, and how is it different from a regular interface?
public static interface Map.Entry<K,V>
This is the definition of the interface. Docs here: Map.Entry<K,V>.
I'm curious about the case when it's not an inner interface.
The static modifier is only allowed on a nested classes or interfaces. In your example Entry is nested inside the Map interface.
For interfaces, the static modifier is actually optional. The distinction makes no sense for interfaces since they contain no code that could access the outer this anyway.
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.
interface A {
void testA();
public interface B {
void testB();
}
}
Finally, even Android Studio indicates that using static with inner interface is not needed:

Categories