Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: declaring field variables

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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 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?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: declaring field variables

    Put an extra pair of {}s around the assignment statement.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    nesthead98 (June 15th, 2012)

  4. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

    in other programs.
    Thanks

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: declaring field variables

    Please post the full text of the error message and the code that caused it.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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 variable myInstance cannot be referenced from a static context

    At Line 128:

    function(MyClass.myInstance);

    Or, if I declare myInstance like this:
    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)

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: declaring field variables

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

    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.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: declaring field variables

    That code compiles with no errors.

    Where is the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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:
    MyClass.myInstance
    myInstance is not a static variable. You need a reference to an instance of the class.
    aRefToClass.myInstance

    Every instance of the MyClass class will have a myInstance variable.

    What are you trying to do?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    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

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: declaring field variables

    If you remove the private for field, it compiles???
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 08:34 PM
  2. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  3. Replies: 3
    Last Post: April 11th, 2011, 09:51 PM
  4. <..> when declaring a class
    By Asido in forum Java Theory & Questions
    Replies: 1
    Last Post: September 8th, 2010, 08:47 AM
  5. Declaring variables in constructor and compiling
    By Newoor in forum Object Oriented Programming
    Replies: 3
    Last Post: December 5th, 2009, 01:07 PM

Tags for this Thread