Read this program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gsreenathreddy
    New Member
    • Oct 2007
    • 40

    Read this program

    package Anonymous;

    class Base
    {
    public void m1(){
    System.out.prin tln("base m1 called");
    }

    }


    package Anonymous;

    class AnonymousTest
    {
    public static void main(String[] args)
    {
    Base b=new Base(){
    public void m1(){
    System.out.prin tln("anonymous m1 called");
    }
    public void m2(){
    System.out.prin tln("anonymous m2 called");
    }
    {
    System.out.prin tln("instane initializer called");
    }
    };
    b.m1();
    }
    }


    Can any body explain abt this program
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Please use code tags when posting code.
    2.) Which part do you want explained? Have you run it to see what the output is?

    Comment

    • gsreenathreddy
      New Member
      • Oct 2007
      • 40

      #3
      [code=java]
      package Anonymous;

      class Base
      {
      public void m1(){
      System.out.prin tln("base m1 called");
      }

      }


      package Anonymous;

      class AnonymousTest
      {
      public static void main(String[] args)
      {
      Base b=new Base(){ //explain here
      public void m1(){
      System.out.prin tln("anonymous m1 called");
      }
      public void m2(){
      System.out.prin tln("anonymous m2 called");
      }
      {
      System.out.prin tln("instane initializer called");
      }
      };
      b.m1();
      }
      }[/code]
      Last edited by JosAH; Oct 30 '07, 09:58 AM. Reason: added [code] ... [/code] tags.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Just posting the code again isn't going to help you much. Did you compile and
        run the code?

        kind regards,

        Jos

        Comment

        • gsreenathreddy
          New Member
          • Oct 2007
          • 40

          #5
          Yes,

          I had compiled and run the application and got the output as

          iinstane initializer called
          m1 is called.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by gsreenathreddy
            Yes,

            I had compiled and run the application and got the output as

            iinstane initializer called
            m1 is called.
            What did you expect as the output?

            Comment

            • gsreenathreddy
              New Member
              • Oct 2007
              • 40

              #7
              I would like what happens internally

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by gsreenathreddy
                Yes,

                I had compiled and run the application and got the output as

                iinstane initializer called
                m1 is called.
                *Which* m1 is called?

                kind regards,

                Jos

                Comment

                Working...