error: Illegal static declaration in inner class asignacion1.sumar [duplicate] - java

I have an inner class that stores the info of the controls I'm using for a game, now I want to store a static ArrayList in it that holds all the names of the controls. But I am getting this error: "Modifier static is only allowed in constant variable declarations"
private class Control{
public static ArrayList<String> keys = new ArrayList<String>();
public final String key;
public final Trigger trigger;
Control(String k, Trigger t){
key = k;
trigger = t;
keys.add(key);
}
}
Now I know this can easily be solved by taking the ArrayList out of the class and storing it in the main class. But I'd prefer to keep all the information in one class where I can access everything.
"Control.key, Control.trigger, Control.keys"
is just more elegant/readable than
"key, trigger, keys"
Or maybe I just have Obsessive–compulsive disorder, still I'd like to do it my way.

You can make the Control class static.
private static class Control {
^^^^^^
// Ok to have static members:
public static ArrayList<String> keys = new ArrayList<String>();
...
This is described in the Java Language Specification Section §8.1.3
8.1.3 Inner Classes and Enclosing Instances
An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers (§8.7) or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields (§15.28).

Make your inner class static and it will work:
private static class Control { ...

Related

Why we are restricted to declare static member variable in inner Class in java?

Consider the below example
Why we are restricted to declare static member variable in inner Class when there isn't any restriction on inheriting static variables in Inner classes?
public class Outer {
public class Inner {
public static String notAllowed;
/* Above line give following compilation error
The field notAllowed cannot be declared static in a non-static inner type, unless initialized with a constant expression
*/
}
}
But now if my inner class extends other class which contains static variable than this works fine.
Consider below code:
public class Outer {
public class Inner extends InnerBase {
/* Since it extends InnerBase so we can access Outer.Inner.allowed */
public Inner(){
Outer.Inner.allowed = null; // Valid statement
}
}
}
public class InnerBase {
public static String allowed;
}
So what is the reason for restricting static variable in inner class as it is achievable through inheritance?
Am I missing something very basic?
From oracle website:
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.
Because an inner class is associated with an instance, it cannot define any static members itself.
I understand it this way:
If inner class have their own static field,and static field have to initialize before class instantiate;
But a innner class only exist with an instance of outterclass ,so it can not initialize its static member before instantiate,then in Contradiction.
Because in order to access the static field, you will need an instance of the Outer class, from which you will have to create an instance of the non-static Inner class.
static fields are not supposed to be bound to instances and therefore you receive a compilation error.
The JLS 8.1.3 specifies:
Inner classes may not declare static initializers or member
interfaces, or a compile-time error occurs.
Inner classes may not declare static members, unless they are constant
variables, or a compile-time error occurs.
It is very much possible that the purpose of declaring a static variable opposes the purpose of declaring ANY variable in an inner class. Static variables are meant to be used statically - in any other static methods and classes, while inner classes imply that those classes serve their outer classes ONLY.
I guess the Java creators just wanted it this way.

double brace initialization and "kind of" static anonymous class

Sometimes for testing I use quick "double-brace" initialization which creates anonymous nested class in Outer class, for example:
static final Set<String> sSet1 = new HashSet<String>() {
{
add("string1");
add("string2");
// ...
}
};
Edit
I am correcting my previously faulty statement that this example keeps reference to Outer instance. It does not and it is effectively equivalent to the following :
static final Set<String> sSet2;
static {
sSet2 = new HashSet<String>() {
{
add("string1");
add("string2");
// ...
}
};
}
both sSet1 and sSet2 are initialized with anonymous nested classes that keep no reference to Outer class.
Does it mean that these anonymous classes are essentially static nested classes ?
As in the related question you are referencing to is discussed, an anonymous class cannot be static technically, but it can be so called effectively static if it is declared in a static context, that is it has no reference to the outer instance.
In your case, however, there is definitely no difference between two approaches, the initialization of static fields is a static context as well.

are the members of a static inner class by default static in java

public static class ViewHolder {
public int a,b;
public void method();
}
are the method and the variables a and b by default static when I declare the class as static or do I have to separately declare them static ? I know its a noobish question but I am a little confused right now :(
No, they're not static by default, they're normal instance members.
Static inner classes, unlike normal inner classes, can have static members, though, if you explicitly declare them.
No, when you declare an inner static class you specify that the declaration itself is static, so that you don't need an object instance of the parent class to access it.
Nothing regarding inner members is involed.
The members of the Static nested class are not static. static keyword is specified with the class which signifies that the nested class can be instantiated with the containing outer class similar to static data member.
BaseClass.StaticNestedClass nestedClass = new BaseClass.StaticNestedClass();
nestedClass.nonStaticMethod();//correct
BaseClass.StaticNestedClass.nonStaticMethod()//Error
This has no effect on the data members of the static nested class which behave as normal class.
Please note if a static keyword is associated with a class then the class has to be a nested class
A public static class works just like any other class. The only real difference is that it is accessed through the containing class:
OuterClass.InnerClass foo = new OuterClass.InnerClass();

Static classes in Java--something is being shadowed

Following is the simplest example of static inner class in Java. Let's look at it.
package staticclass;
final class Outer
{
final public static class Inner
{
static String s = "Black";
}
static Extra Inner = new Extra(); //The inner class name and the object name of the class Extra are same and it is responsible for shadowing/hiding Inner.s
}
final class Extra
{
String s = "White";
}
final public class Main
{
public static void main(String[] args)
{
System.out.println(Outer.Inner.s);
}
}
Within the Outer class, there is a static class named Inner and a static object with the same name Inner of type Extra. The program displays Whilte on the console, a string in the Extra class through Outer.Inner.s in main() which is intended to display the string contained in the Inner class within the Outer class
Why is there no name collision between a static inner class and a static object of type Extra? How does the Extra class enjoys the higher priority than the Inner class and displays the string contained in the Extra class?
This is what the JLS says. which I believe applies to your case:
6.3.2 Obscured Declarations
A simple name may occur in contexts where it may potentially be
interpreted as the name of a variable, a type or a package. In these
situations, the rules of §6.5 specify that a variable will be chosen
in preference to a type, and that a type will be chosen in preference
to a package.
So since your static Extra Inner = new Extra() is a variable, it will be chosen over final public static class Inner which is a type.
The quite more elaborate paragraph 6.3.1 specifies how shadowing generally is to be resolved.
Because the inner class actual type name is Outer$Inner. It's a convenience that you can reference it as Inner. I imagine the compiler knows this and takes the more explicit rule for the local variable.

Why are you not able to declare a class as static in Java?

Why are you not able to declare a class as static in Java?
Only nested classes can be static. By doing so you can use the nested class without having an instance of the outer class.
class OuterClass {
public static class StaticNestedClass {
}
public class InnerClass {
}
public InnerClass getAnInnerClass() {
return new InnerClass();
}
//This method doesn't work
public static InnerClass getAnInnerClassStatically() {
return new InnerClass();
}
}
class OtherClass {
//Use of a static nested class:
private OuterClass.StaticNestedClass staticNestedClass = new OuterClass.StaticNestedClass();
//Doesn't work
private OuterClass.InnerClass innerClass = new OuterClass.InnerClass();
//Use of an inner class:
private OuterClass outerclass= new OuterClass();
private OuterClass.InnerClass innerClass2 = outerclass.getAnInnerClass();
private OuterClass.InnerClass innerClass3 = outerclass.new InnerClass();
}
Sources :
Oracle tutorial on nested classes
On the same topic :
Java: Static vs non static inner class
Java inner class and static nested class
Top level classes are static by default. Inner classes are non-static by default. You can change the default for inner classes by explicitly marking them static. Top level classes, by virtue of being top-level, cannot have non-static semantics because there can be no parent class to refer to. Therefore, there is no way to change the default for top-level classes.
So, I'm coming late to the party, but here's my two cents - philosophically adding to Colin Hebert's answer.
At a high level your question deals with the difference between objects and types. While there are many cars (objects), there is only one Car class (type). Declaring something as static means that you are operating in the "type" space. There is only one. The top-level class keyword already defines a type in the "type" space. As a result "public static class Car" is redundant.
Class with private constructor is static.
Declare your class like this:
public class eOAuth {
private eOAuth(){}
public final static int ECodeOauthInvalidGrant = 0x1;
public final static int ECodeOauthUnknown = 0x10;
public static GetSomeStuff(){}
}
and you can used without initialization:
if (value == eOAuth.ECodeOauthInvalidGrant)
eOAuth.GetSomeStuff();
...
You can create a utility class (which cannot have instances created) by declaring an enum type with no instances. i.e. you are specificly declaring that there are no instances.
public enum MyUtilities {;
public static void myMethod();
}
Sure they can, but only inner nested classes. There, it means that instances of the nested class do not require an enclosing instance of the outer class.
But for top-level classes, the language designers couldn't think of anything useful to do with the keyword, so it's not allowed.
public class Outer {
public static class Inner {}
}
... it can be declared static - as long as it is a member class.
From the JLS:
Member classes may be static, in which case they have no access to the instance variables of the surrounding class; or they may be inner classes (§8.1.3).
and here:
The static keyword may modify the declaration of a member type C within the body of a non-inner class T. Its effect is to declare that C is not an inner class. Just as a static method of T has no current instance of T in its body, C also has no current instance of T, nor does it have any lexically enclosing instances.
A static keyword wouldn't make any sense for a top level class, just because a top level class has no enclosing type.
As explained above, a Class cannot be static unless it's a member of another Class.
If you're looking to design a class "of which there cannot be multiple instances", you may want to look into the "Singleton" design pattern.
Beginner Singleton info here.
Caveat:
If you are thinking of using the
singleton pattern, resist with all
your might. It is one of the easiest
DesignPatterns to understand, probably
the most popular, and definitely the
most abused.
(source: JavaRanch as linked above)
In addition to how Java defines static inner classes, there is another definition of static classes as per the C# world [1]. A static class is one that has only static methods (functions) and it is meant to support procedural programming. Such classes aren't really classes in that the user of the class is only interested in the helper functions and not in creating instances of the class. While static classes are supported in C#, no such direct support exists in Java. You can however use enums to mimic C# static classes in Java so that a user can never create instances of a given class (even using reflection) [2]:
public enum StaticClass2 {
// Empty enum trick to avoid instance creation
; // this semi-colon is important
public static boolean isEmpty(final String s) {
return s == null || s.isEmpty();
}
}
Everything we code in java goes into a class. Whenever we run a class JVM instantiates an object. JVM can create a number of objects, by definition Static means you have the same set of copy to all objects.
So, if Java would have allowed the top class to be static whenever you run a program it creates an Object and keeps overriding on to the same Memory Location.
If You are just replacing the object every time you run it whats the point of creating it?
So that is the reason Java got rid of the static for top-Level Class.
There might be more concrete reasons but this made much logical sense to me.
The only classes that can be static are inner classes. The following code works just fine:
public class whatever {
static class innerclass {
}
}
The point of static inner classes is that they don't have a reference to the outer class object.
I think this is possible as easy as drink a glass of coffee!.
Just take a look at this.
We do not use static keyword explicitly while defining class.
public class StaticClass {
static private int me = 3;
public static void printHelloWorld() {
System.out.println("Hello World");
}
public static void main(String[] args) {
StaticClass.printHelloWorld();
System.out.println(StaticClass.me);
}
}
Is not that a definition of static class?
We just use a function binded to just a class.
Be careful that in this case we can use another class in that nested.
Look at this:
class StaticClass1 {
public static int yum = 4;
static void printHowAreYou() {
System.out.println("How are you?");
}
}
public class StaticClass {
static int me = 3;
public static void printHelloWorld() {
System.out.println("Hello World");
StaticClass1.printHowAreYou();
System.out.println(StaticClass1.yum);
}
public static void main(String[] args) {
StaticClass.printHelloWorld();
System.out.println(StaticClass.me);
}
}
One can look at PlatformUI in Eclipse for a class with static methods and private constructor with itself being final.
public final class <class name>
{
//static constants
//static memebers
}
if the benefit of using a static-class was not to instantiate an object and using a method then just declare the class as public and this method as static.

Categories

Resources