0

I got an error that says "non-static variable constantInteger cannot be referenced from a static context"

How do I add all three integers without changing their level?

    public class VariableAdder {

      final int constantInteger = 5;
      int instanceInteger = 10;

      public static void main(String[] args) {

        int methodInteger = 20;
        int result = constantInteger + instanceInteger + methodInteger;
        System.out.println(result);

      }

    }

Expected results: adding two instance variables and one method variable and printing. Actual results: error

Thank you all for your help!

  • Declare constantInteger and instanceInteger as static and access them with class name in your main method ,or create an object of VariableAdder and then access them with object – Shubham Dixit Feb 08 '19 at 05:15

3 Answers3

3

You cannot use an Instance variable(Class level, non static , final/non final) inside a static method(main() in your case).

Instantiating VariableAdder and using the instance to refer to the instance variables will solve your problem. Something like this:

public class VariableAdder {

    final int constantInteger = 5;
    int instanceInteger = 10;

    public static void main(String[] args) {

        VariableAdder t = new VariableAdder();

        int methodInteger = 20;
        int result = t.constantInteger + t.instanceInteger + methodInteger;
        System.out.println(result);

    }


If you are not looking for creating an instance, declare constantInteger and instanceInteger as static and access them directly inside the main()

Monil Panchal
  • 328
  • 2
  • 12
  • 1
    Just to clarify: that last bit about `static`s, while technically correct, doesn't apply in OP's case; by the requirement of the assignment, OP will have no choice but to declare them non-static and access them through an instance of `VarriableAdder`. – Kevin Anderson Feb 08 '19 at 05:37
1

An instance variable and an instance constant means you need an instance in order to access them. For your code example this means creating an instance of class VariableAdder. To create an instance you call a constructor. The following runs successfully with JDK 11.0.2 on Windows 10

public class VariableAdder {

    final int constantInteger = 5;
    int instanceInteger = 10;

    /** Constructor */
    public VariableAdder() {
    }

    public static void main(String[] args) {
        int methodInteger = 20;

        // Create an instance of class 'VariableAdder'
        VariableAdder adder = new VariableAdder();

        int result = adder.constantInteger + adder.instanceInteger + methodInteger;
        System.out.println(result);
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
0

This should work:

final static int constantInteger = 5;
static int instanceInteger = 10;

public static void main(String[] args) {

    int methodInteger = 20;
    int result = constantInteger + instanceInteger + methodInteger;
    System.out.println(result);

}

EDIT : OR

final static int constantInteger = 5;
static int instanceInteger = 10;

public static void main(String[] args) {

    MyClass runner = new MyClass();

    int ti=runner.instanceInteger;
    int methodInteger = 20;
    int result = constantInteger + instanceInteger + methodInteger;
    System.out.println(result);

}
Bush
  • 261
  • 1
  • 11
  • It'll work, but doesn't comply with the instructions in the assignment that call for using an _instance_ variable and an _instance_ constant. – Kevin Anderson Feb 08 '19 at 05:38