declaring field variables
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:
Code :
public class MyClass {
public static final MyClass myInstance = new MyClass() { field = value };
// declaration of field & constructor for MyClass
}
but that doesn't work. Is this possible / does anyone have any ideas?
Re: declaring field variables
Put an extra pair of {}s around the assignment statement.
Re: declaring field variables
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
in other programs.
Thanks
Re: declaring field variables
Please post the full text of the error message and the code that caused it.
Re: declaring field variables
If I have this as the declaration:
Code :
public final MyClass myInstance = new MyClass() {{ field = value }};
I get this error:
File: /.../MyOtherClass.java [line: 128]
Error: non-static variable myInstance cannot be referenced from a static context
At Line 128:
function(MyClass.myInstance);
Or, if I declare myInstance like this:
Code :
public static final MyClass myInstance = new MyClass() {{ field = value }};
I get this this error:
File: /.../MyClass.java [line: 11]
Error: non-static variable field cannot be referenced from a static context
(where line 11 is the declaration of myInstance)
Re: declaring field variables
Please post a small, complete program that I can compile and get these errors from.
Two lines of code can't be compiled to get the errors.
Re: declaring field variables
Code :
public class MyClass {
private int field;
public final MyClass myInstance = new MyClass() {{ field = 3; }};
}
Code :
public class MyOtherProgram {
public void function(MyClass mc) {
}
public void start() {
function(MyClass.myInstance);
}
public static void main() {
MyOtherProgram mop = new MyOtherProgram();
mop.start();
}
}
The start function is there so that we don't get any errors due to the fact that main is a static function, and also to better emulate the actual, complicated programs I'm trying to write.
Re: declaring field variables
That code compiles with no errors.
Where is the problem?
Re: declaring field variables
I get this error:
1 error found:
File: /.../MyOtherProgram.java [line: 8]
Error: non-static variable myInstance cannot be referenced from a static context
Re: declaring field variables
Whoops, I used the old version of MyClass with a static final class instance.
The following is the way to reference static variables in a class: myInstance is not a static variable. You need a reference to an instance of the class.
Every instance of the MyClass class will have a myInstance variable.
What are you trying to do?
Re: declaring field variables
I would like it to be a static variable. If I do this:
Code :
public class MyClass {
private int field;
public static final MyClass myInstance = new MyClass() {{ field = 3; }};
}
I get this error instead:
1 error found:
File: /.../MyClass.java [line: 4]
Error: non-static variable field cannot be referenced from a static context
Re: declaring field variables
If you remove the private for field, it compiles???