Cannot make a static reference to the non-static field memberVariable with private variable

前端 未结 3 877
一生所求
一生所求 2020-12-31 02:30

I created a enum with one private member variable. When i try to access the member variable the compiles states \'Cannot make a static reference to the non-static field memb

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 02:53

    The main cause of error is : you are trying to make reference to private variable (memberVariable) of super class from inner implementation class.

    To make error free code you can do either of following :

    • you have to use super.memberVariable since memberVariable is not local to TheOnlyAndOnly()
    • you can make int memberVariable public.
    • you can do:

      TheOneAndOnly(1) {
          int memberVariable=4;
          @Override
          public int addValue(final int value) {
              return memberVariable + value;
          }
      };
      

提交回复
热议问题