简体   繁体   中英

sharing variables in java between classes

I have three static methods each in their own public classes. Simple arithmetic methods to help me learn.

The methods use three variables that when defined as static int variables in the main method. The program works.

Now according to my understanding of what I have read, I should be able to move the variable definition to another class and define them as public. The main method in its own public class should then be able to find these definitions and run. But this does not happen. Instead my eclipse workspace reports that the definitions cannot be resolved to a variable.

Here is the very simple code (which gives me the compilation error):

package christmas;

public class addintegers {
    public int number1 = 5;
    public int number2 = 10;
    public int answer;

    public static int add2numbers(int a, int b) {
        return (a + b);
    }
}

class 2

package christmas;

public class subtractintegers {
    public static int sub2numbers(int a, int b) {
        return (a - b);
    }
}

Then I have my main method. This is where I am getting the compilation error.

package christmas;

public class glue {
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println(addintegers.add2numbers(number1, number2));
        System.out.println(subtractintegers.sub2numbers(number1, number2));
        answer = (addintegers.add2numbers(number1, number2)) + (subtractintegers.sub2numbers(number1, number2));
        System.out.println("answer =" + answer);
    }
}

the errors I get are:

>create local variable number1
>create local variable number2
>create local variable answer

First off:

  • Class names should be nouns, and use UpperCamelCase
  • Method names should be verbs, and use lowerCamelCase

Eg, something along the lines of:

public class Addition {

    public static int add(int a, int b) {
        return a + b;
    }
}
public class Subtraction {  

    public static int subtract(int a, int b) {
        return a - b;
    }    
}

If you want to have number1 , number2 , and answer fields in the class that I renamed Addition , normally what you would do is make them private instance variables and expose them through getters:

public class Addition {

    private int number1 = 5;

    public int getNumber1() {
        return number1;
    }
}

You can then access them by creating an instance of the class, eg: new Addition().getNumber1(); . Since the Addition and Subtraction classes seem to be utility classes however, perhaps it makes more sense to declare these fields private static so you won't have to instantiate the classes:

public class Addition {

    private static int number1 = 5;

    public static int getNumber1() {
        return number1;
    }
}

You can then access them by referencing the class as opposed to an instance of the class, eg: Addition.getNumber1(); .

You are almost there. You are missing 2 key points:

  • To share the variables number1, number2 and answer without instantiating the addIntegers class, you need to make them static.
  • To access the variables number1, number2 and answer from within the glue class' main method, you need to prefix the variables with the addIntegers class.
    package christmas;

    public class addintegers {
        public static int number1 = 5;
        public static int number2 = 10;
        public static int answer;

        public static int add2numbers(int a, int b) {
            return (a+b);
        }
    }
package christmas;

public class glue {


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println(addintegers.add2numbers(addintegers.number1,addintegers.number2));
        System.out.println(subtractintegers.sub2numbers(addintegers.number1,addintegers.number2));
        addintegers.answer =     (addintegers.add2numbers(addintegers.number1,addintegers.number2))+(subtractintegers.sub2numbers(addintegers.number1,addintegers.number2));
        System.out.println("answer =" +addintegers.answer );
    }

}
package com.rohov;

public class Application {
    public static void main(String[] args) {
        System.out.println(AddInteger.sum(AddInteger.num1, AddInteger.num2));
        System.out.println(SubInteger.sub(AddInteger.num1, AddInteger.num2));
        AddInteger.sum = AddInteger.sum(AddInteger.num1, AddInteger.num2) + SubInteger.sub(AddInteger.num1, AddInteger.num2);
        System.out.println(AddInteger.sum);
    }
}

class AddInteger {
    public static int num1 = 5;
    public static int num2 = 10;
    public static int sum;

    public static int sum(int a, int b) {
        return a + b;
    }
}

class SubInteger {
    public static int sub(int a, int b) {
        return a - b;
    }
}

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