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.
requires that the Boo class has a constructor with String parameter type.Boo f = new Boo("24");
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().but then what happens to the value passed in the constructor.
what is the significance of the value passed in the constructor.
The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
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 ?
The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
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.
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
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 .
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.
Don't get me started about those stupid light bulbs. |