Okay, i have two classes. One needs to access (public) methods within the other . If i declare

ClassB objectName = new ClassB();

in one method of class A , then use objectName.methodname() in that method then it works fine.

Butif i then try and call objectName.methodname() from another method in class A it doesnt work, i need to make another instance of it.

Can i just make one instance of ClassB which all the methods in ClassA can access?

Does this make sense?

Assign that class instance to an instance variable, rather than a local variable, if you want to use that same instance throughout the class.

I.E.

public class A {
    private B bObject;

    A() {
      bObject = new B();
    }

    void whateverOne() {
        bObject.whatever();
    }

    void whateverTwo() {
        bObject.whatever();
    }
}

rather than

public class A {
    A() {
    }

    void whateverOne() {
        B bObject = new B();
        bObject.whatever();
    }

    void whateverTwo() {
        B bObject = new B();
        bObject.whatever();
    }
}
Comments
fast reply
This question has already been answered. Start a new discussion instead.