Jump to content

Can't Understand the Exact Sequence of method Calling

- - - - -

  • Please log in to reply
3 replies to this topic

#1
codemink

codemink

    Newbie

  • Members
  • Pip
  • 1 posts
I'm new to Java after having a good working experience of 5 years over C++.
Like C++, where the program starts its execution through main() function and proceeds further by calling other functions, I can't figure out this sequence in Java.

I covered most of the topic in Java, and when I came to AWT, I got the folowing problem:


    import java.awt.*;  

    public class MenuExample extends Frame {  

     String menuSelection = "Select a menu item.";   

     public static void main(String args[]){  

      MenuExample win = new MenuExample();  

     }  

     public MenuExample() {  

      super("MenuExample");  

      pack();  

      resize(400,400);  

      addMenus();  

      show();  

     }  

     void addMenus() {  

      MenuBar menubar = new MenuBar();  

      Menu file = new Menu("File");  

      Menu edit = new Menu("Edit");  

      Menu view = new Menu("View");  

      file.add("Open");  

      file.add("Save");  

      file.add("Close");  

      file.add("Quit");  

      edit.add("Copy");  

      edit.add("Cut");  

      edit.add("Paste");  

      view.add("Zoom");  

      menubar.add(file);  

      menubar.add(edit);  

      menubar.add(view);  

      setMenuBar(menubar);  

     }  

     public void paint(Graphics g) {  

      g.drawString(menuSelection,100,100);  

     }  

     public boolean handleEvent(Event event) {  

      if(event.id==Event.WINDOW_DESTROY){  

       System.exit(0);  

       return true;  

      }else if(event.id == Event.ACTION_EVENT &&   

        event.target instanceof MenuItem){  

       if("Quit".equals(event.arg)){  

        System.exit(0);  

        return true;  

       }else{  

        menuSelection = "You selected "+event.arg.toString()+".";   

        repaint();  

        return true;  

       }  

      }else return false;  

     }  

    }  


I can't figure out the sequence in which the above programming code works. I means, starting from main(), constructor is invoked. then......!!!???

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Why don't you put println() method into every method, to print out the exact sequence?
.

#3
LukeyJ

LukeyJ

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Println is the best bet, but all that code does is call the constructor. So, follow the logic in the constructor and that's the order of execution. Once that is done, it will finish. I think.


public MenuExample() { super("MenuExample"); pack(); resize(400,400); addMenus(); show(); }
This calls the parent constructor with a string parameter. The calls the following 4 methods in order.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Java starts execution in the static main method of the class with the name that was called by the command line. For example, if you launch it with "java MyApp.class", it looks for the main method in class MyApp.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users