Home / Java Programming / Declarations and Access Control :: Discussion

Discussion :: Declarations and Access Control

  1. What will be the output of the program?

    public class A {      void A() /* Line 3 */     {         System.out.println("Class A");      }      public static void main(String[] args)      {          new A();      }  } 

  2. A.
    Class A
    B.
    Compilation fails.
    C.
    An exception is thrown at line 3.
    D.
    The code executes with no output.

    View Answer

    Workspace

    Answer : Option D

    Explanation :

    Option D is correct. The specification at line 3 is for a method and not a constructor and this method is never called therefore there is no output. The constructor that is called is the default constructor.


Be The First To Comment