aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Local Classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static Local Classes?" Watch "Static Local Classes?" New topic
Author

Static Local Classes?

Amandeep Waraich
Ranch Hand

Joined: Jul 14, 2000
Posts: 56
This is Marcus Test #1 Qn no. 30
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

My answer is 1,2..but the answer given is 1,2,3. Khalid's book clearly states on page 238:
"A local class cannot be specified with the keyword static."
Yeah, I know that it also states that if the context is static, then the local class is implicitly static, but does this mean that you CAN use the keyword static if the class is implicitly static?
Tbanks,
Aman

rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
does compiler reject "static" for the above example?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi Amandeep Waraich
The answer are indeed 1,2 & 3 as this is a top level nested class not a local inner class. note the close of braces of main method given below in bold
public class MyClass1 {
public static void main(String argv[]){ }
/*Modifier at XX */ class MyInner {}
}

However this is not possible and will give a compile error
public class MyClass1 {
public static void main(String argv[]){
static class MyInner {}
}
}
The above class MyInner is inside a method main
Regds
Rahul

[This message has been edited by rahul_mkar (edited July 24, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The Correct answeres are 1,2 and 3. Assume inner class as a
member of the top-level class. So private, public and static are
legal modifiers for members.
Regards
Hemant.
 
wood burning stoves
 
subject: Static Local Classes?
 
Similar Threads
Modifiers for Local Inner classes
Marcus Green's Mock question
Marcus green exam 1 Question 30
Inner Class Accessibiltiy
Inner Class definition per JLS