'How can i call the method from another class?

Hi i am trying to solve the problem I am facing

public class exam {
    public static void main(String[] args) {
        test1 a = new test1();
        
       }
    int zahl(int x, int y) {
        int e;
        if(x>y) {
            e=x-y;
        }else {
            e=y-x;
        }
        if(e==0) {
            return 0;
        }
        int z=0;
        int i=1;
        while(i<=e) {
            z=z+i;
            i++;
        }
        return z;
    }

}

what I want to do is to call the zahl method to the test1 class

public class test1{
    private exam b;
    
    public void init() {
        b = new exam();
    }
    void test() {
        int result = b.zahl(2, 2);
        assertEquals(1, result);
    }

}

this is what I have tried, but it returns nothing, even though it's supposed to show me error.



Solution 1:[1]

You should probably be declaring your functions with the public tag i.e. public void test() if you intend to access them from other functions outside of that package. The usual Class naming convention in Java is with capital first letter, which makes your code more readable for you and others.

For your question, I don't think you are actually invoking the test() method of the test1 class. If you want that method to get called every time, you could place it inside the default Constructor.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 borazan