5 Replies Latest reply: Dec 3, 2010 6:14 AM by YoungWinston RSS

    Calling a method from a different class.

    813296
      Assume we have this scenario:
       Class tree
      {
      
         tallTree( )
         {
            ....
          }
      
      }
      
      Class House
      { 
          house( )
          {
             ....
           }
      }
      In class tree is it possible to call the method house in class "house"

      would it be house.tallTree()? I was reading online that the get operator can call methods.

      In that case would this be correct. tallTree()(current class from where we are calling).House(class we want to call).gethouse()(the method we are calling);
        • 1. Re: Calling a method from a different class.
          804650
          810293 wrote:
          I was reading online that the get operator can call methods.
          As far as I am aware there is no such thing as a get operator in Java. Perhaps you mean getter method or more appropriate accessor method.

          If you want to call a method in another class then you need a reference to that class. That is you need to either create an instance of class B in class A or have an instance of class B passed to class A.
          class Foo {
              Bar bar = new Bar();
          
              public void methodOne() {
                  bar.someMethod();
              }
          
              public void methodTwo(Bar b) {
                  b.someMethod();
              }
          }
          • 2. Re: Calling a method from a different class.
            813296
            Ah I see Pinto that cleares a lot of doubt.

            Correct me If I am wrong.
            class Foo {
               // We create a Bar class in class foo. Because the Bar class is already made and hence we are using it or should I say creating one for class foo to use.
                Bar bar = new Bar();
             
                public void methodOne() {
               // from the Bar we made the name we give it in this case "bar" does a method in methodOne().
                    bar.someMethod();
                }
             
                public void methodTwo(Bar b) {
              // I am 75% sure I know what this is doing. In the methodTwo parameters we create a "Bar" (Bar and give it the name of "b")
                    b.someMethod();
                }
            }
            Edited by: 810293 on Dec 2, 2010 8:31 PM
            • 3. Re: Calling a method from a different class.
              804650
              Just to clarify a few things Objects do not have names, variables do.
              Bar barney = new Bar();
              This has created a variable called barney. That variable can reference a Bar object. Make sure you understand the difference.

              I guess I was assuming a bit much from you when I posted the above code. Add the below code and see if that makes sense.
              class Test {
                  public static void main(String[] args) {
                      Bar boozer = new Bar();
                      Foo fooey = new Foo();
                      fooey.methodTwo(boozer);
                  }
              }
              On that last line it is passing a reference to a Bar object (the one created 2 lines above) to the method as a parameter. The object is not passed. Make sure you understand the difference here as well.
              • 4. Re: Calling a method from a different class.
                813296
                "I guess I was assuming a bit much from you when I posted the above code. Add the below code and see if that makes sense."
                Does this mean the parts I commented were incorrect?
                class Test {
                    public static void main(String[] args) {
                    // Bar creates a new object named "boozer"      <--- for the "Bar" is that Bar class and we are just creating a new Bar object in class Test?
                        Bar boozer = new Bar();
                    // Foo creates a new object named "fooey"
                        Foo fooey = new Foo();
                   // fooey uses methodTwo and references/calls boozer
                        fooey.methodTwo(boozer);
                    }
                }
                I really appreciate your help and im glad I found someone that teaches this simply. I shall Ace my test thanks to your guidance! :)
                • 5. Re: Calling a method from a different class.
                  YoungWinston
                  810293 wrote:
                  Does this mean the parts I commented were incorrect?
                  Not sure but your comments in the bit you posted are misleading, eg:
                    // Bar creates a new object named "boozer" ...
                  Bar boozer = new Bar();
                  'Bar' doesn't create anything; +'new Bar()'+ does. It creates a new object of type Bar.
                  A better explanation would be:
                  "This statement creates a new Bar object and puts it in a variable called 'boozer'."

                  A few other pointers for you:
                  1. For variables declared outside methods (eg, the 'bar' variable in your 'Foo' class), get used to putting a qualifier in front of them, viz:
                  private Bar bar = new Bar();
                  and, unless you've got a very good reason not to, always make it private.

                  2. When you're making up examples, try to make them mean something. Unfortunately, many books still insist on using 'Foo' and 'Bar' and meaningless method names. In fact, as you go further, you'll discover that giving your variables and methods good names is one of the most important things you can do to make your programs readable.
                  I really appreciate your help and im glad I found someone that teaches this simply. I shall Ace my test thanks to your guidance! :)
                  We were all there once :-) . Good luck on your test.

                  Winston