[Solved] Where do I define my variables? [closed]


you have to create an instance of your class in order to access non static variables from your static methods in java.

public class MainGUI
{
  int num1= 1366, num2= 528, num3= 482, sum; // declare these static?

  public static void main(String args[])
  {
MainGui m = new MainGUI();
    sum = m.num1 + m.num2+ m.num3; 
  }
}

or make your instance variables static so that you can access them directly without any instance from your static methods .

  public class MainGUI
    {
      static int num1= 1366, num2= 528, num3= 482, sum; // declare these static?

      public static void main(String args[])
      {
        sum = num1 + num2+ num3; 
      }
    }

though, by convention static variables from static methods should be accessed with classname.variablename

      sum =  MainGUI.num1 + MainGUI.num2 + MainGUI.num3; 

please refer to this link about better understanding of different types of variable access

1

solved Where do I define my variables? [closed]