Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8865153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:25:57+00:00 2026-06-14T16:25:57+00:00

So in my little Java program I had declared all my variables in the

  • 0

So in my little Java program I had declared all my variables in the main method, but I wanted to be able to modify them from anywhere inside the class, so I declared them in the class body instead. Now I am not able to modify them inside the main method anymore. Is the solution for this to declare the variables static? If I were to do so would I still be able to change those variables from inside the main method?

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

  public static void main(String args[])
  {
    sum = num1 + num2+ num3; //compiler tells me "non-static varable cannot be refrenced from a static context"
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:25:58+00:00Added an answer on June 14, 2026 at 4:25 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I moved to Objective C (iOS) from C++ (and little Java) I had
I'm writing a little Java program (it's an ImageJ plugin, but the problem is
I need to be able to ssh from a Java program into a remote
I'm trying to make a little java program that executes a system command but
All, I'm going through the Friedman & Felleisen book A Little Java, A Few
Had to write the following program for an on line pre java class using,
I have to write a little Java program that stands in the middle of
I do most of my development in Java and C++ but recently had to
I am creating a GUI using Java. This GUI launches a program from the
I have to code a Java program that will receive messages from network and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.