How is the method being called here?

Posted on 2011-10-28
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
    LVL 35

    Accepted Solution

    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

    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 35

    Expert Comment

    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.

    Author Comment

    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 35

    Expert Comment

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Suggested Solutions

    Title # Comments Views Activity
    scoresIncreasing challenge 10 47
    scoresAverage challenge 8 58
    WMI, model #, retrieving information 10 50
    wordmultiple challenge 12 62
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now