Vswe's Summer Courses | You're not logged in. Sign up or log in | Server time: | Donate
One can now see other persons' badge hunts. Simply head over to their profiles to access them.

Go back to course page

Coding and a cup of Java

An introduction course to programming

Variable Access

  • 2013-06-25 00:03:37
    I have a general java question here. I am doing the assignment from The Coding and a Cup of Java Course, and need a way to access a variable (in this case an int of how much money is left, the prices of the products, and an array of the products that can be bought) and was wondering if there is a way to access these variables and arrays in many methods within the same class.
  • 2013-06-25 00:11:37
    Take a look at this example program and you'll see how to do it.

    public class MyExampleProgram {
        static int myVariable;

        public static void main(String[] args) {
            System.out.println(myVariable);
            myMethod();
            System.out.println(myVariable);
        }

        static void myMethod() {
            myVariable = 3;
        }
    }
  • Log in or sign up to reply to this thread