• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

How is the method being called here?

In the program below,  in line 3 why don't they just write doit()? Why are they creating an instance of the TestIt class before they call a method inside TestIt?
public class TestIt {
  public static void main(String a[]){
     new TestIt().doit();
     /*
      output :
      Hello world!
     */
  }
  public void doit() {
      new InnerClass().sayHello();
  }
  public void enclosingClassMethod(){
      System.out.println("Hello world!");
  }
 
 
 class InnerClass {
   public void sayHello() {
     TestIt.this.enclosingClassMethod();
   }
 }
}

Open in new window

0
shampouya
Asked:
shampouya
  • 3
  • 3
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
That's because the doIt() method is an instance method (not a static method), and so it HAS to be called on an instance of TestIt. In this example, doIt() has to be an instance method because of what this code is trying to demonstrate, but in another context, you could declare doIt() like so...

public static void doIt() {

and then you could white just doIt(); on line 3.
0
 
shampouyaAuthor Commented:
Oh I see, so in a case like this writing the keyword static in the method signature would make it simpler to call the doIt() method?
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
As I said, in this particular example, you wouldn't be able to do that because of what is happening inside the doIt() method. The example REQUIRES that the doIt() method be an instance method.

But if you were writing your own code for something different than this example, then yes you could use the static keyword and you would be able to call it as I mentioned.

Note: this is a very trivial example, designed to demonstrate an inner class accessing an enclosing class' method. In a non-trival example, where you are writing code to do something purposeful, there would be very good design reasons to make some methods static and other methods instance methods. Your words of "make it simpler to call" wouldn't really influence the way in which you declare your methods.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
shampouyaAuthor Commented:
I see. Can you tell me, without getting into too much detail, why something like doIt() has to be an instance method? And also, which of these is possible?

1. Calling static methods from a static method like main
2. Calling instance methods from a static method like main
3. Calling static methods from instance methods
4. Calling instance methods from instance methods
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Firstly, all are possible except number 2. Mind you, this is in the case where you are just referencing the method from within the same class, like above, and without qualifying the method call. By that, I mean that on line 3, you are calling an instance method from a static method, but you can do that because you are giving it an instance (new TestIt()) to call the method on.

And for your first question, doIt() has to an instance method because it refers to InnerClass which is an instance class. And similarly, InnerClass is an instance class because it refers to an instance of the TestIt class on line 19 (due to the part of the line, TestIt.this.)
0
 
shampouyaAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now