*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Class Accessibiltiy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner Class Accessibiltiy" Watch "Inner Class Accessibiltiy" New topic
Author

Inner Class Accessibiltiy

manav kher
Ranch Hand

Joined: Nov 21, 2000
Posts: 98
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 answer given is 1,2 which i disagree with
But a local class cannot have a static keyword(though it is implicitly static bcz it is inside a static block)
Also it cannot have any accessibility modifier.
manav
Rajiv Ranjan
Ranch Hand

Joined: Sep 28, 2000
Posts: 61
Hi Manav ,
May I ask why you disagree with the answers?
Best bet would be to add the modifiers , compile it and see for yourself.
Rajiv
manav kher
Ranch Hand

Joined: Nov 21, 2000
Posts: 98
Sorry the answers are 1,2 and 3
But as i mentioned in my last post local classes cannot have any accessibility modifiers (hence 1 and 2 are incorrect choices for the question) and it cannot contain the "static" keyword and hence i don't think 3 is right
manav
Morgan Subram
Greenhorn

Joined: Jan 03, 2001
Posts: 12
public class ClassTest{

public static class Inner{
public void good() {
int i=0;
}
private static class InnerInner{
private static class InnerInnerInner {
private static void k() {
System.out.println("In InnerInnerInner static method");
}

}

}
}


public static void main(String [ ] argc){
ClassTest.Inner.InnerInner.InnerInnerInner c= new ClassTest.Inner.InnerInner.InnerInnerInner();
c.k();
}
}
Consider this piece of code and you will agree
George Toronto
Ranch Hand

Joined: Dec 30, 2000
Posts: 78
Hi, Morgan, Manav
Your example is a good one for understanding the top-level nested class, but it is not right one to undertsand the current question which is about local inner class.
As a local inner class, just as its name hints us, is a local variable or at least, we can take them as local variable. Based this rules, we can undersatnd the question easily. A local variable cannot have any accessibility including public, private, so 1,2 are wrong. static keyword is not permitted to modifier an local inner class. so no.3 is also wrong.
Chapter 7 in Khalid's book decipted these concepts concisely and correctly, plz refer to it.
regds
George

[This message has been edited by George Toronto (edited January 04, 2001).]
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
George you are write, but the question at hand is not a local inner class. George and manav, take a look and you will see that the main method is closed with {} and then you have the inner class. So class MyInner is not declared inside of a method. Thus the correct answers are 1,2,3.
Bill
George Toronto
Ranch Hand

Joined: Dec 30, 2000
Posts: 78
Oh, yeah. I am sorry about my mistake. But I really understand that difference between the Inner class and local inner class.
I am sorry, Morgan. sorry for all of friends who have see my last statement. I will not make same mistake twice.
regds
George
[This message has been edited by George Toronto (edited January 04, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner Class Accessibiltiy
 
Similar Threads
Marcus green exam 1 Question 30
Static Local Classes?
modifier for inner class
Marcus Green's Mock question
static modifiers