Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
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
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Programmer Certification (OCPJP)
confused question
Mellihoney Michael
Ranch Hand
Posts: 124
posted 21 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I want to ask the diffrence of the following code:
(Inner is an class enclosed by class Outer)
code A:
Inner i=new Inner();//that ok
Outer.Inner oi=new Outer().new Inner();//only if Inner is static inner class?
a beginner in java
Mellihoney Michael
Ranch Hand
Posts: 124
posted 21 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Inner i=new Outer().new Inner();//what about this one?
a beginner in java
Sridhar Srikanthan
Ranch Hand
Posts: 366
posted 21 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Michael , look at this code and ask me if you have any questions ?
class Outer { public static void main(String [] args) { //this works only when you are instantiating from the enclosing class Inner i=new Outer().new Inner();//that ok //below line if uncommented will give you a compiler error //because as Inner class is like any other instance memeber //of the Outer class, you have to reference it through outer //class instance. //Inner i=new Inner();// not ok...compiler error new Outer().aMethod(); // this is also for non-static inner class Outer.Inner oi=new Outer().new Inner();//only if Inner is static inner class? //this is the way we instantiate inner static classes Outer.StaticInner os = new Outer.StaticInner(); //This works for instantiating a static inner class only when // you are instantiating from the enclosing class; StaticInner os1 = new Outer.StaticInner(); } void aMethod() { //here it is ok to instantiate Inner this way because //it is being accessed from an instance method. Inner i=new Inner();//that ok Inner j = new Outer().new Inner();//this is also ok } static class StaticInner { StaticInner() { System.out.println("You are in static inner"); } } class Inner { Inner() { System.out.println("I am in inner class"); } } }
Hope this helps
Sri
Sarma Lolla
Ranch Hand
Posts: 203
posted 21 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
public class Outer{ Outer(){ System.out.println("Outer"); } static class StaticInner{ StaticInner(){ System.out.println("StaticInner"); } } class Inner{ Inner(){ System.out.println("Inner"); } } public static void main(String args[]){ Outer.StaticInner staticInner= new Outer.StaticInner(); Outer.Inner inner= new Outer().new Inner(); } }
Take a look at this code
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
use of Outer.this
Inner Class
Inner classes
Inner Class - Initialization Sequence
Inner Class
More...