-1

I'm in the process of learning Java and trying to use OOP style programming. The problem I'm having is that when I attempt to call a getter method in one class, for another class, it only works within the method that I create an object in and I'm calling from. My question is: Is it possible to call a getter from multiple methods in a different class than the getter method class? If so, how do I go about it?

Thanks!

  • 1
    This is so vague dude... can't really say anything there. Give us concrete questions and concrete programs. – Stefan Reich Dec 07 '17 at 01:37
  • 4
    Show us the minimum amount of code to demonstrate your problem. Real, but minimum. – Elliott Frisch Dec 07 '17 at 01:37
  • Possible duplicate of [How to call a method function from another class?](https://stackoverflow.com/questions/26269193/how-to-call-a-method-function-from-another-class) –  Dec 07 '17 at 01:41

2 Answers2

0

if I am not mistake you wanna try call different methods from different classes right?

public class A {
    public void getMethodFromClassA(){
       ....
    }
}

public class B {
   public void getMethodFromClassB(){
     ........
   }
}

call the method

public class C {

    A a = new A();
    B b = new B();

   private void methodCaller(){

      a.getMethodFromClassA();
      b.getMethodFromClassB();
   }
}
Gusti Arya
  • 1,145
  • 2
  • 12
  • 29
  • Yeah, Gusti. You're on the right track, but haven't gone quite far enough. I want to call a getter method from a different class, but not from the same method where I create the object. Thanks for helping! – Dan Bennett Dec 07 '17 at 02:28
  • @DanBennett so do you want to call other other class getter method, but not in the same method where that other class object is created? – Gusti Arya Dec 07 '17 at 02:51
  • That looks like what I want to do. I'll code it and try it out. Thanks! – Dan Bennett Dec 07 '17 at 03:01
  • Gusti, I think that fixed my problem. Thanks a lot! – Dan Bennett Dec 07 '17 at 04:10
0

The class with getter method:

public class A {
    public void getter(){
       ....
    }
}

The class that calls the getter method:

public class B {

    A a = new A();

    private void callMethod(){
        a.getter();
    }
}

If the class that calls the getter method includes the main method, than make the create a with the static keyword: static A a = new A();.

Caders117
  • 319
  • 3
  • 17