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:
Tim Cooke
Campbell Ritchie
Ron McLeod
Junilu Lacar
Liutauras Vilda
Sheriffs:
Paul Clapham
Jeanne Boyarsky
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Piet Souris
Carey Brown
Bartenders:
Jesse Duncan
Frits Walraven
Mikalai Zaikin
Forum:
Programmer Certification (OCPJP)
creating obj for innerclass.....
Ganeshkumar cheekati
Ranch Hand
Posts: 362
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
SOURCE:
www.danchisholm.net
class A { private static int counter; public static int getCounter(){return counter++;} private static int innerCounter; public static int getInnerCounter(){return innerCounter++;} private String name; A() {name = "A" + getCounter();} class B { private String name; B() { name = "B" + getInnerCounter(); System.out.print(A.this.name + name); }} void m1() {new A().new B();} // 1 [B]void m2() {new A.B();} // 2[/B] void m3() {new B();} // 3 public static void main(String[] args) { A a1 = new A(); a1.m1(); a1.m2(); a1.m3(); }}
here line-2 is similar to the syntax of creating an object for static inner classes...
why it is allowed here?
[ December 11, 2008: Message edited by: Ganeshkumar cheekati ]
[ December 11, 2008: Message edited by: Ganeshkumar cheekati ]
SCJP5 and SCWCD1.5
Think Twice Act Wise...
Ankit Garg
Sheriff
Posts: 9693
42
I like...
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Ganesh if you have the other post that you gave, the compiler automatically inserts the this reference when you create an instance of a non-static inner class in a non-static method of the enclosing class. So the code
void m2() {new A.B();}
is equivalent to
void m2() {
this.
new A.B();}
SCJP 6 | SCWCD 5 |
Javaranch SCJP FAQ
|
SCWCD Links
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Q on Inner classes
inner class
doubt in Inner class...?
need explanation on the result
Inner classes
More...