How is the method being called here?

Posted on 2011-10-28
Medium Priority
Last Modified: 2012-05-12
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() {

Open in new window

Question by:shampouya
  • 3
  • 3
LVL 36

Accepted Solution

mccarl earned 2000 total points
ID: 37047592
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.

Author Comment

ID: 37047686
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?
LVL 36

Expert Comment

ID: 37047918
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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 37047970
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
LVL 36

Expert Comment

ID: 37048033
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.)

Author Closing Comment

ID: 37049037

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question