be a well encapsulated person, don't expose your privates, unless you public void getWife()!
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
All code in my posts, unless a source is explicitly mentioned, is my own.
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Stephen Davies wrote:Just to add to Rubens point, static inner classes are actually treated by the JVM as a top-level class.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
"static inner classes aren't even supposed to be called inner classes at all. Technically, they are "top-level nested classes"
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Stephen Davies wrote: I am uncertain if they can recieve the private and protected access modifiers especially if they are treated as top-level classes.
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Stephen Davies wrote: I suppose, its to do with the fact that even though they are inner classes they remain attrubutes of the outer class, and thus may be protected as such.
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Stephen Davies wrote:But isn't i just a reference variable, to an Inner object?
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
My doubt is HOW can we say inner, without the class name of Outer.( Inner class is a non -static class)
![]()
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Ankit Garg wrote:
Also Stephen you are right that static inner classes are treated like top level classes so they must not have private accessibility. And indeed the static inner classes or any other inner classes have no private or protected accessibility. What happens is a trick done by the compiler by changing the accessibility of the constructor of the static innre class. I had experimented on this using decompilation and reflection some time ago but I'm unable to recall it right now...
All code in my posts, unless a source is explicitly mentioned, is my own.
.Ankit Garg wrote: well James the code that you gave will not work
campfire wrote:You can also instantiate both the outer class and inner class at the same time:
Inner i = new Outer().new Inner();
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
class Outer{
class Inner{}
}
class Test{
public static void main(String[] args){
new Test().fun();
}
void fun(){
Outer.Inner i= new Outer().new Inner(); // yes wee need Outer.Inner i
}
}
Regards
Sunny Mattas
SCJP5
If we create the inner class object in outer class then
1. Inner i= new Outer().new Inner();
will work.If I am wrong kindly correct me.
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
you don't need a outer class object to create an inner class object inside the outer class.
To create an instance of an inner class, you must have an instance of the outer class
to tie to the inner class. There are no exceptions to this rule: an inner class instance
can never stand alone without a direct relationship to an instance of the outer class.
Regards
Sunny Mattas
SCJP5
Sunny Mattas wrote: I think we still need a outer class object to create an inner class object inside the outer class when our inner class is not static
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
James Tharakan wrote:
class outer{
inner i= new inner();
class inner{
}
}
It is not always requried to have a outer class object to create a inner class object.
Regards
Sunny Mattas
SCJP5
Sunny Mattas wrote: I think we still need a outer class object to create an inner class object inside the outer class when our inner class is not static
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
All code in my posts, unless a source is explicitly mentioned, is my own.
Ruben Soto wrote:
Ankit Garg wrote:
Also Stephen you are right that static inner classes are treated like top level classes so they must not have private accessibility. And indeed the static inner classes or any other inner classes have no private or protected accessibility. What happens is a trick done by the compiler by changing the accessibility of the constructor of the static innre class. I had experimented on this using decompilation and reflection some time ago but I'm unable to recall it right now...
Nice discussion, guys.![]()
Ankit, what do you mean by the statements marked in bold? I'm afraid that you lost me there. Can't proper (non-static) inner classes be marked private and protected? Also, how does the constructor discussion apply here? I'm just missing what you are trying to say.![]()
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
All code in my posts, unless a source is explicitly mentioned, is my own.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
All code in my posts, unless a source is explicitly mentioned, is my own.
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |