0

I'm new to programming, I need a little help understanding how to use the same variable in many methods. Thanks in advance!

I have a class with some static variable declared before the methods, after I have a first method which calculate some value and a second method which print the results. In the printing method I want to print the initial input but it's always 0. Can someone help me to understand how to update the global variable please?

I tried to return the value from the method but it doesn't update the "global" variable

import java.util.Scanner;

public class Main {
    public static boolean evenNumber = false;
    public static boolean positiveNumber = false;
    public static boolean positiveEvenNumber = false;
    public static int intInputNumber;


    public static void main(String[] args) {
            testEvenPositive();
            printMethod();
        }

        public static int testEvenPositive(){
            System.out.println("Please insert a number, it will be evaluated to even or not and id it's positive or not.");
            Scanner scanner = new Scanner(System.in);
            int intInputNumber = Integer.parseInt(scanner.nextLine());
            if (intInputNumber > 0) {
                positiveNumber = true;}
            if (intInputNumber % 2 == 0) {
                evenNumber = true;}
            if (intInputNumber % 2 == 0 && intInputNumber > 0) {
                positiveEvenNumber = true;}

            return intInputNumber;
        }

        public static void printMethod() {
            System.out.println(Main.intInputNumber + " is an even number: " + evenNumber);
            System.out.println(Main.intInputNumber + " is a positive number: " + positiveNumber);
            System.out.println(Main.intInputNumber + " is a positive even number: " + positiveEvenNumber);
        }
    }
1
  • 2
    In your testEvenPositive you're never changing the value of the field - you're using a newly-declared local variable. If you change int intInputNumber = ... to just intInputNumber = ... in the method that will fix the problem. I generally wouldn't use static fields like this, but I understand this is just while you're learning. Commented Jan 13, 2022 at 15:41

1 Answer 1

5

The problem is that you have declared a local variable (intInputNumber) in your testEvenPositive() method. That is what is being changed. You are also ignoring what is being returned. You could instead not declare a local variable and make the method void

public static void main(String[] args) {
            testEvenPositive();
            printMethod();
        }

        public static void testEvenPositive(){
            System.out.println("Please insert a number, it will be evaluated to even or not and id it's positive or not.");
            Scanner scanner = new Scanner(System.in);
            intInputNumber = Integer.parseInt(scanner.nextLine());
            if (intInputNumber > 0) {
                positiveNumber = true;}
            if (intInputNumber % 2 == 0) {
                evenNumber = true;}
            if (intInputNumber % 2 == 0 && intInputNumber > 0) {
                positiveEvenNumber = true;}
        }

or you could return the variable and then assign it in the main method

public static void main(String[] args) {
            intInputNumber = testEvenPositive();
            printMethod();
        }

        public static int testEvenPositive(){
            System.out.println("Please insert a number, it will be evaluated to even or not and id it's positive or not.");
            Scanner scanner = new Scanner(System.in);
            int intInputNumber = Integer.parseInt(scanner.nextLine());
            if (intInputNumber > 0) {
                positiveNumber = true;}
            if (intInputNumber % 2 == 0) {
                evenNumber = true;}
            if (intInputNumber % 2 == 0 && intInputNumber > 0) {
                positiveEvenNumber = true;}

            return intInputNumber;
        }

I am also of the opinion that you shouldn't use global variables in this case. You can define the variables in your main methods and pass parameters.

Sign up to request clarification or add additional context in comments.

2 Comments

Just to add: declaring intInputNumber within testEvenPositive() while there is a static field with the same name is called variable shadowing or hiding (the local variable hides the field within its scope).
Thanks! I tried to not declare a local variable in testEvenPositive but if I do that the compiler wont find the variable in main :S. I solved putting the scanner in main and passing intInputNumber in the other methods as a parameters! Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.