• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Marcus Exam 1 question

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code :



<pre>

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.
********************
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Raj Mehra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
He's my best friend. Not yours. Mine. You can have this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic