Q 30) from Marcus Green Exam 1 public class MyClass1 { public static void main(String argv[]){ } /*Modifier at XX */ class MyInner {} } What modifiers would be legal at XX in the above code? 1) public 2) private 3) static 4) friend The correct answers given are 1,2 and 3. Can someone explain this.
public class MyClass1 { public static void main(String argv[]){ } /*Modifier at XX */ class MyInner {} } What modifiers would be legal at XX in the above code? 1) public 2) private 3) static 4) friend
</pre>
Hi Raj, The class <code> class MyInner {} </code>defined inside the class <code> public class MyClass1{} </code> is essentially a nested class. The nested class can be Top level nested class or inner class. The difference is toplevel nested classes are static and inner classes are not. All access specifiers can be applied to these classes.(coderanch, private,protected or no modifier). and one access specifier(i.e. static) can be applied.
So the answers. Hope this helps. Regards ----- vadiraj
------------------ ******************** There's a lot of I in J. ********************
Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
public class MyClass1 { public static void main(String argv[]){ } /*Modifier at XX */ class MyInner {} } What modifiers would be legal at XX in the above code? 1) public 2) private 3) static 4) friend The correct answers given are 1,2 and 3. Can someone explain Note that the class is outside the method main (ie it is not local), therefore it can be a static-inner(static can be used) class or a non-static innner class and both of these can have any visibility modifiers(coderanch, protected, private & none (default)) (refer the table in Khalid Mughal's book on classes/inner classes). Obviously the 4th option is not valid. Hope this will help you.