Search:

Type: Posts; User: nesthead98

Search: Search took 0.11 seconds.

  1. Replies
    11
    Views
    2,073

    Re: declaring field variables

    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; }};

    }
  2. Replies
    11
    Views
    2,073

    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
  3. Replies
    11
    Views
    2,073

    Re: declaring field variables

    public class MyClass {
    private int field;

    public final MyClass myInstance = new MyClass() {{ field = 3; }};

    }


    public class MyOtherProgram {
  4. Replies
    11
    Views
    2,073

    Re: declaring field variables

    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...
  5. Replies
    11
    Views
    2,073

    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

    MyClass.myInstance
    ...
  6. Replies
    11
    Views
    2,073

    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:



    public class...
Results 1 to 6 of 6