0

this question will seem very dumb to many, but I seem to lack some basic understanding of Java. I just never used several classes in one project. For a bigger project I want to split the code on a few classes, which have to operate each with one another. Here is a very basic example for better understanding.

I have the three classes TestMain, TestA and TestB. TestMain reads in two Strings input1 and input2. TestA parses input1 to the int inputA, TestB parses input2 to the int inputB. TestA then uses inputA and inputB to produce result1, and than TestB uses result1 to create result2. After that TestMain prints out both results.

I am not sure if this is even possible, since the classes have to interact with each other simultaneously. Since this is necessary for my other project, that's my first question - is this even possible?

My code until now is this, but there are several problems with it:

public class TestMain {

public static void main(String[] args) throws IOException {

    String input1, input2;
    TestA testA = new TestA(); TestB testB = new TestB();

    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    input1 = br.readLine();
    input2 = br.readLine();
    System.out.println("A: " + testA.result1 + ", B: " + testB.result2);
}
}

public class TestA {

    public static void main(String args[]){

        String inputA;
        int a; int result;

        TestMain testMain = new TestMain(); TestB testB = new TestB();

        inputA = testMain.input1;
        a = Integer.parseInt(inputA);
        result1 = a*testB.b;
    }   
}

public class TestB {

    public void main(String args[]){

        String inputB;
        int b;
        int result2;

        TestMain testMain = new testMain(); TestA testA = new testA();

        inputB = testMain.input1;
        b = Integer.parseInt(inputB);
        result2 = testA.result1*2;
    }
}

That I can't solve this very easy example shows my lack of basics, so I hope that you can help me. Cheers, Joshflux

1 Answer 1

2

You don't have 3 real classes, you instead have 3 repositories for static main methods, a pretty worthless situation. For you to create classes that can work together, you must first learn to leave the static realm and into the instance realm, create classes that have instance fields and methods, including fields of other classes such as your classes. Hit the books and tutorials and learn OOPs as it applies to Java.

The question really means that you haven't yet studied OOPs and need to remedy this -- simple as that. Start here

Answered as a Community Wiki because I don't want rep for this one.

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

1 Comment

Thank you, I'll look into it :).

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.