简体   繁体   中英

Java - using static class variables in various methods

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);
        }
    }

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;}

            return intInputNumber;
        }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM