• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

inner class?? (Topic deleted by mistake)

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I repost this question from srikanth reddy because I deleted it by mistake - sorry.
-Barry

What is the result 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
h. Other compile-time error.
i. Run-time error
j. None of the above

i think the answer to be e & f ..but the answer is given as c..
can anyone explain how ???

thanks & regards

srikanth
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This looks like one of Dan Chisholm's questions. Which one is it? Usually Dan gives detailed answers to such questions. Have you read Dan's answer? What specifically don't you understand?
 
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
A nested class is a class defined as a member of another class. Its type is Enclosing.Nested (and its filename is Enclosing$Nested.class). If a nested class is non-static, then it is an "inner" class, because it can only be created "inside" an instance of the enclosing class.

In a static context (that is, with no "this" reference to an instance of the enclosing class), any new instance of the inner class must be explicitly tied to a particular instance of the enclosing class. This can be done in two ways:

Enclosing.Inner x = new Enclosing().new Inner();

-or-

Enclosing x = new Enclosing();
Enclosing.Inner y = x.new Inner();

However, in the context of an implicit "this" reference to an instance of the enclosing class (that is, within an instance method of the enclosing class), inner class objects can also be created as follows (where "new" is implicitly this.new):

Enclosing.Inner x = new Inner();

Note that multiple instances of the inner class can be created with a single instance of the enclosing class.

So, in this case...

A a1 = new A(); A new instance of A is created and assigned to a1. In creating this object, static variables "counter" and "innerCounter" are both initialized to 0; then in A's constructor, an instance variable "name" is set to "A0", and "counter" is incremented to 1.

new A().new B(); First, another new instance of A is created. Using the same static variable "counter", a new instance variable "name" is set to "A1", and "counter" is incremented to 2. Next, an instance of A.B is created as an inner object associated with the instance of A just created. In B's constructor, a different instance variable "name" -- this one restricted to the scope of the inner class -- is set to "B0", and innerCounter is incremented to 1. The print statement outputs A's "name" (A1) followed by B's "name" (B0).

new A.B(); Here, a new instance of A.B is created without creating a new instance of A. Instead, the enclosing instance is the object referenced by a1 ("this" in m2). In B's constructor, another instance variable "name" is set to "B1", and innerCounter is incremented to 2. The print statement outputs a1's "name" (A0) followed by B's "name" (B1).

new B(); This is essentially a shorthand version of above. Another instance of A.B is created, also associated with the instance of A referenced by a1 ("this" in m3). In B's constructor, another instance variable "name" is set to "B2", and innerCounter is incremented to 3. The print statement outputs a1's "name" (A0) followed by this B's "name" (B2).
[ September 06, 2005: Message edited by: marc weber ]
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks marc...

i wanted to know one more thing ..like
for non-static method you need to create an instance of enclosing class before you create for the inner one as ..
enclosing.inner innerinstance=new enclosing().new inner();

and for static inner class u create by...

enclosing.inner innerinstance=new enclosing.inner();

so my doubt since the innerclass is non-static how can we use the line 2 for creating the instance of inner class..please this was the confusion i have .so i thought the answer could be compiler error at line 2 but it is not so..

thanks & regards

srikanth
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What was wrong with Dan's answer?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic