• 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

q on inner classes

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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); // 1
}}
static void m1() {new A().new B();} // 2
static void m2() {this.new B();} // 3
static void m3() {new B();} // 4
public static void main(String[] args) {
m1(); m2(); m3();
}}

What are the results of attempting to compile and run the program?
a. Prints: A0B0A1B1A1B2
b. Prints: A0B0A1B1A2B2
c. Prints: A1B0A0B1A0B2
d. Compile-time error at line 1
e. Compile-time error at line 2
f. Compile-time error at line 3
g. Compile-time error at line 4

answer:: a

i have a query why does 3 compile wat is 'this' in 3 since method is static
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steven,

Are you sure the given code snippet is compiling?
It must throw a compile-time error at line 3 and 4.

Check your answer again.

Thanks,
Praveen
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by PraveenPonna:
...It must throw a compile-time error at line 3 and 4...


Agreed.
 
steven gerrard
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm thanks a lot i didnt compile it earlier
its not compiling
clears my doubt
i guess ans given in mock(http://www.danchisholm.net) is not correct
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,
I cant understand the //1 line. Wat Doet "A.this.name" means? A is the class name and generally we cal only static variables using Class names. I have never encountered a syntax like this. Can anyone Explain?
Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic