• 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

problem in anonymous inner classes

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Which create an anonymous inner class from within class Bar? (Choose all that apply.)
A. Boo f = new Boo(24) { };
B. Boo f = new Boo() {String s; };
C. Bar f = new Boo(String s) { };
D. Boo f = new Boo.Bar(String s) { };

this is the question from kb6 book page. no 692-693 from inner classes. the correct answer of the above question is B. i would like to know that what if the A option was something like this Boo f = new Boo("24"); or for that matter any we pass any string in the constructor. what would that have meant. anonymous inner class creates subclass of the declared type . that means if we do Boo f = new Boo("24") , it will create a sublcass of Boo and will instantiate it, so that f points to this just in time created object of the anonymous subclass of Boo. but then what happens to the value passed in the constructor. what is the significance of the value passed in the constructor.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote: i would like to know that what if the A option was something like this Boo f = new Boo("24"); or for that matter any we pass any string in the constructor. what would that have meant. anonymous inner class creates subclass of the declared type . that means if we do Boo f = new Boo("24") , it will create a sublcass of Boo and will instantiate it, so that f points to this just in time created object of the anonymous subclass of Boo. but then what happens to the value passed in the constructor. what is the significance of the value passed in the constructor.


Nothing happens to the String passed in the constructor. Normally, parameters passed in the constructor are stored in instance fields, but in this case, since there is no code in the constructor, the value is not used for anything. This instance of Boo will never know that the String "24" ever existed.
 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. The correct answer is B. Boo f = new Boo() {String s; }; Hope it is obvious. If you don't find it, re-look at the syntax of other options carefully.
2.

Boo f = new Boo("24");

requires that the Boo class has a constructor with String parameter type.
3.

but then what happens to the value passed in the constructor.

You find Boo(String s) { } in the Boo class. So variable s stores this string 24, which is a number type. But to use it as a number type, use methods like parseInt().
4.

what is the significance of the value passed in the constructor.


The significance lies in what you do to above point 3. Use it as you wish.

Hope that clarifies.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so that means if in the options it was Boo f = new Boo("24") { }; that would also be counted as correct option ? right ?

secondly i would like to know is that when i declared anonymous boo subclass in zoo method as shown below:



i would like to know how to create constructor of the anonymous class ?
 
Rajdeep Biswas
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:so that means if in the options it was Boo f = new Boo("24") { }; that would also be counted as correct option ? right ?




Yes, you have a Boo constructor that has String parameter. Place only 24 in place of "24", you'll get error.

To create constructor, you need name. Do you have name? No, then how can you create constructor. You can use non-static or instance blocks for initializing instance variables in that anonymous inner class if required.

Check the following code, compile it, and find what .class files are generated. Then in command prompt, use javap command for each class to find out what members are in there.


Gurpreet, hope this helps your basic understanding.

Experts please help here.

> javap Barr$1



1. What is this final variable? Its requirement?
2. Why the constructor has two parameters? Their requirement?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Check the following code, compile it, and find what .class files are generated. Then in command prompt, use javap command for each class to find out what members are in there.



When i disassembled the code i found the following output :

C:\Users\G\Desktop>javap Barr$1
Compiled from "Barr.java"
class Barr$1 extends Boo{
final Barr this$0;
Barr$1(Barr, java.lang.String);
}

Since inner classes have implicit reference to the outer/enclosing class so i think the Barr variable is just because of that. And the string variable is the one which we have used while constructing anonymous inner class object. though i'm not very much clear about how the constructor is generated. what if the anonymous inner class defines its own instance variables. how would/can they be initialized. ? Will wait for experts to post

 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:

Check the following code, compile it, and find what .class files are generated. Then in command prompt, use javap command for each class to find out what members are in there.



When i disassembled the code i found the following output :

C:\Users\G\Desktop>javap Barr$1
Compiled from "Barr.java"
class Barr$1 extends Boo{
final Barr this$0;
Barr$1(Barr, java.lang.String);
}

Since inner classes have implicit reference to the outer/enclosing class so i think the Barr variable is just because of that. And the string variable is the one which we have used while constructing anonymous inner class object. though i'm not very much clear about how the constructor is generated. what if the anonymous inner class defines its own instance variables. how would/can they be initialized. ? Will wait for experts to post




Method local and anonymous inner classes, which are defined in a scope with a this variable, have an enclosing outer class. So, yes, the first param is for the outer class reference. Also, normally, when you create a class, and don't define a constructor, all you get is a default no-args constructor -- this won't work here as you need a constructor that takes a string and calls the superclass constructor with that string. So that is done for you as well.

Henry
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


thanks for the reply Henry but i'm still confused. if i ask the difference between following two statements , what will be the explanation ?

Boo f = new Boo("24") { };
Boo f = new Boo(){};

Assume that the above two declaration of anonymous classes goes inside zoo method , independently .
 
Henry Wong
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:thanks for the reply Henry but i'm still confused. if i ask the difference between following two statements , what will be the explanation ?

Boo f = new Boo("24") { };
Boo f = new Boo(){};

Assume that the above two declaration of anonymous classes goes inside zoo method , independently .




What do think the difference is? It's kinda obvious. Take a guess, and odds are you will be correct.

Tell us what you think the difference is, and we can give you a hint in the right direction.

Henry
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let me try. in both cases we are declaring and instantiating anonymous inner class which will be subclass of the declared type i.e. subclass of Boo type. In the first scenario viz Boo f = new Boo("24") {}; , considering output from javap , it will have a constructor call with 2 arguments. one of type Barr (which is the enclosing class) and one of java.lang.String. since anonymous inner class is subclass of Boo, it will inherit the String instance variable from Boo class and will assign the string passed (Boo f = new Boo("24")) to the 2-arg constructor.

in the second scenario viz Boo f = new Boo() {}; there will be one-arg constructor , with a reference to Barr as always.
 
Henry Wong
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gurpeet singh wrote:let me try. in both cases we are declaring and instantiating anonymous inner class which will be subclass of the declared type i.e. subclass of Boo type. In the first scenario viz Boo f = new Boo("24") {}; , considering output from javap , it will have a constructor call with 2 arguments. one of type Barr (which is the enclosing class) and one of java.lang.String. since anonymous inner class is subclass of Boo, it will inherit the String instance variable from Boo class and will assign the string passed (Boo f = new Boo("24")) to the 2-arg constructor.

in the second scenario viz Boo f = new Boo() {}; there will be one-arg constructor , with a reference to Barr as always.




You are definitely overthinking the problem -- the enclosing outerclass parameter of the constructor is an implementation detail. This is true for regular inner classes, along with method local and anonymous inner classes that have the "this" variable in scope. If you hadn't done a javap call, you wouldn't even have know about it.... It would be a good idea to pretend that it didn't exist.

Basically, the first case, the compiler created a constructor that takes a string, and whose implementation is simply a call to the super constructor that takes a string. In the second case, the compiler created a no-arg constructor, whose implementation is simply a call to the superclass no-arg constructor.

Henry
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks henry. i got the tricky details from your posts. right now I'm starting with Threads chapter from kb6 book but i have heard about your book too. would definitely get my hands dirty in it.
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic