• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner Class Accessibiltiy

 
manav kher
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • 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 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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic