I would like it to be a static variable. If I do this:
public class MyClass {
private int field;
public static final MyClass myInstance = new MyClass() {{ field = 3; }};
}
Type: Posts; User: nesthead98
I would like it to be a static variable. If I do this:
public class MyClass {
private int field;
public static final MyClass myInstance = new MyClass() {{ field = 3; }};
}
I get this error:
1 error found:
File: /.../MyOtherProgram.java [line: 8]
Error: non-static variable myInstance cannot be referenced from a static context
public class MyClass {
private int field;
public final MyClass myInstance = new MyClass() {{ field = 3; }};
}
public class MyOtherProgram {
If I have this as the declaration:
public final MyClass myInstance = new MyClass() {{ field = value }};
I get this error:
File: /.../MyOtherClass.java [line: 128]
Error: non-static...
Alright, I was able to declare the instance, but I wasn't able to edit the fields unless I made the instance non-static. Any solution to that? I'd like to be able to call
MyClass.myInstance
...
Hi,
I would like to declare a static final class instance, and set one of its field variables in the same line (all outside of a method). Ideally, it'd look something like this:
public class...