Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2010
    Location
    India
    Posts
    10
    Rep Power
    0

    Default Order of EXECUTION followed by JVM!

    Java Code:
    class Main {
    
        static String throwMeThat(String myString) {
            System.out.println(myString);
            return myString;
        }
    
        public static void main(String[] args) {
            new Third();
        }
    }
    
    class First {
    
        String var1inFirst = Main.throwMeThat("**I am var1inFirst**");
        String var2inFirst = Main.throwMeThat("**I am var2inFirst**");
    
        public First() {
            Main.throwMeThat("**I am the constructor in class First**");
        }
    }
    
    class Second extends First {
    
        String var1inSecond = Main.throwMeThat("**I am var1inSecond**");
        String var2inSecond;
    
        public Second() {
            Main.throwMeThat("**I am the constructor in class Second**");
        }
    }
    
    class Third extends Second {
    
        String var1inThird;
        {
            var1inThird = Main.throwMeThat("**I am var1inThird**");
        }
        String var2inThird;
        String var3inThird = Main.throwMeThat("**I am var3inThird**");
    
        public Third() {
            Main.throwMeThat("**I am the constructor in class Third**");
            var2inThird = Main.throwMeThat("**I am var2inThird**");
            var2inSecond = Main.throwMeThat("**I am var2inSecond**");
        }
    }
    I didn't understoOD the order of execution followed by JVM!! :confused:Can any explain to me. Thanx in advance.
    “Give somebody a fish and they eat for a day.Teach somebody to fish they eat for life!”

  2. #2
    Join Date
    Aug 2010
    Location
    India
    Posts
    10
    Rep Power
    0

    Default

    I mean when "new Third()" statement is executed.. I always assumed that the constructor Third() is called. But here assumptions are contradicted.. Plz help..
    “Give somebody a fish and they eat for a day.Teach somebody to fish they eat for life!”

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,958
    Rep Power
    19

    Default

    What about the order don't you understand?
    What do you get as an output, and what did you expect as an output?

  4. #4
    Join Date
    Aug 2010
    Location
    India
    Posts
    10
    Rep Power
    0

    Default

    Actually I read in the Sun tutorials that when you call the constructor of the subclass the no-arguement constructor of the superclass is called automatically...

    Now accordingly the output I expected was::
    **I am var1inThird**
    **I am var3inThird**
    **I am the constructor in class First**
    **I am the constructor in class Second**
    **I am the constructor in class Third**
    **I am var2inThird**
    **I am var2inSecond**

    all I want to know is that "when a subclass's constructor is called where the control of execution is transferred??"
    “Give somebody a fish and they eat for a day.Teach somebody to fish they eat for life!”

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    where the control of execution is transferred
    Do the print outs from the constructors show that?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by _ShivamKapoOr_ View Post
    I mean when "new Third()" statement is executed.. I always assumed that the constructor Third() is called. But here assumptions are contradicted.. Plz help..
    No, when a ctor is called, first a superclass ctor is called (either explicitly or implicitly), next the intializers are called and only then the body of the ctor is executed.

    kind regards,

    Jos

Similar Threads

  1. thread execution one after another
    By turanan in forum New To Java
    Replies: 16
    Last Post: 05-08-2012, 12:11 PM
  2. Help in execution of a Timer
    By Alice.B in forum Threads and Synchronization
    Replies: 1
    Last Post: 05-27-2010, 06:36 AM
  3. .BAT execution error
    By hunterbdb in forum Advanced Java
    Replies: 5
    Last Post: 02-23-2009, 05:41 AM
  4. JAR execution
    By patoh in forum New To Java
    Replies: 2
    Last Post: 11-26-2008, 09:09 AM
  5. Execution cut
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 06-27-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •