Josh
Josh

Reputation: 113

Passing variables to different classes

I'm sure there is an extremely simple solution for this but I'm desperately struggling to find the answer. I'm trying to set the value from a main class into classA then retrieve that value in classB and do something else with it before returning the answer in the Main:

Example:

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ClassA classA = new ClassA();
        ClassB classB = new ClassB();

        System.out.println("Number: ");
        classA.setX(input.nextInt());

        System.out.println("Total: " + classB.printTotal());

    } // main
} // class

Class A:

public class ClassA {
    int x;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }
} // class

ClassB:

public class ClassB {
    ClassA classA;

    public int printTotal() {
        int y = classA.getX() * 5;
        return y;
    }
} // class

Whatever I input returns null. I understand why this is the case but I don't know the solution.

Upvotes: 1

Views: 185

Answers (6)

Yogendra Singh
Yogendra Singh

Reputation: 34387

In main, set classA variable of classB with classA object, rest should be fine.

If your both the classes are in the same package, then you can simply assign the object as below and you are done.

    ClassA classA = new ClassA();
    ClassB classB = new ClassB();
    classB.classA = classA;//assign classA to ClassB's classA attribute

otherwise:

    ClassA classA = new ClassA();
    ClassB classB = new ClassB();
    classB.setClassA(classA);//assign classA to ClassB's classA attribute

and in ClassB, add a setter method as:

     public void setClassA(ClassA classA){
        this.classA = classA;
     }

Upvotes: 1

coder
coder

Reputation: 5400

you need to add to ur main before printing the value.

 classB.classA = classA;

so the classA initialized in your classb can have the x value you set in your main. Hope this helps good luck!

Upvotes: 0

Summer_More_More_Tea
Summer_More_More_Tea

Reputation: 13446

In class B, classA should refer to an object of A.

Include a constructor in class B

public B(A a) {
    this.classA = a;
}

In the main method, initialize classB with classB = new B(classA);.

Upvotes: 0

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

Your ClassB should define the dependency on ClassA either through constructor or a (setter) method:

public ClassB(ClassA classA) {
    this.classA = classA;
}

//or/and

public void setClassA(ClassA classA) {
    this.classA = classA;
}

And inject the dependency in main as follows:

ClassB classB = new ClassB(classA);

Upvotes: 2

awolfe91
awolfe91

Reputation: 1647

Rather than having an instance of ClassA in ClassB, perhaps you should pass in a ClassA object as a parameter to printTotal:

public class ClassB {
    public int printTotal(ClassA classA) {
        int y = classA.getX() * 5;
        return y;
    }
} // class
...

System.out.println("Total: " + classB.printTotal(classA));

Hope that helps!

Upvotes: 1

rekire
rekire

Reputation: 47975

Your ClassB has no reference to an instance of ClassA. So you can use again a setter or maybe better set the instance of ClassA with the constructor:

public ClassB(ClassA instance) {
   this.classA = instance;
}

Upvotes: 0

Related Questions