• 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

Inner class....

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:
1. public class ThisClass {
2. private String mine = new String("mine");
3. public class ThatClass {
4. public void yourMeth() {
5. //method body
6. }
7. }
8. }
What should you use to instantiate ThatClass from outside its containing class?
A
ThatClass tc = new ThisClass.ThatClass();

B
ThatClass tc = ThatClass.new ThatClass();

C
ThisClass tc = new ThisClass();
ThisClass.ThatClass thc = tc.new ThisClass();

D
ThisClass tc = tc.new ThisClass();
tc.ThatClass thc = new tc.ThatClass();

The Answer given is C.
My argument is the that class is never instantiated... It is only the this class that is instantiated with a ref. to that class....
Any supportive stmts..
Thanks
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that you have a typo in C. Shouldn't it read
C
ThisClass tc = new ThisClass();
ThisClass.ThatClass thc = tc.new ThatClass();
 
nt novel
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No I havent made any typo..
Unless the answers are themselves typed wrong !!!
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the answer itself must be typed wrong. To make an instance of the inner class it should be, as Cindy said:
ThisClass tc = new ThisClass();
ThisClass.ThatClass thc = tc.new ThatClass();
or, all on one line:
ThatClass tc = new ThisClass().new ThatClass();
At least I'm 99% sure that is th right syntax for all on one line. If not someone please correct me.
Dave
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't it as simple as
ThisClass.ThatCLass = new ThisClass.ThatClass();
Let me know if I am wrong.
Roopa

Originally posted by nt novel:
Given:
1. public class ThisClass {
2. private String mine = new String("mine");
3. public class ThatClass {
4. public void yourMeth() {
5. //method body
6. }
7. }
8. }
What should you use to instantiate ThatClass from outside its containing class?
A
ThatClass tc = new ThisClass.ThatClass();

B
ThatClass tc = ThatClass.new ThatClass();

C
ThisClass tc = new ThisClass();
ThisClass.ThatClass thc = tc.new ThisClass();

D
ThisClass tc = tc.new ThisClass();
tc.ThatClass thc = new tc.ThatClass();

The Answer given is C.
My argument is the that class is never instantiated... It is only the this class that is instantiated with a ref. to that class....
Any supportive stmts..
Thanks


 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roopa Bagur:
Isn't it as simple as
ThisClass.ThatCLass = new ThisClass.ThatClass();
Let me know if I am wrong.
Roopa



In your line you're trying to call a method named ThatClass(). To get a new ThatClass object you have to say new ThatClass().
Also in your line above you dont assign it to a variable to hold the new object (I assume that's just a typo). Someone correct me if I'm wrong.
Dave
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roopa, I believe your answer would be correct (assuming you added an identifier to the left operand) if ThatClass were static.
However, since it is not, it requires an instance of the outer class to serve as a reference. If you need to keep a handle to that reference, you would have to use the corrected code Cindy listed. Otherwise, you could use a single line expression:
ThisClass.ThatClass thc = new ThisClass().new ThatClass();
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can check www.geocities.com/korayguclu/
if you want to learn more on inner classes.
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,
The correct is (c) below:

You may verify the result from the best teacher in coding , yeah, I am referring to the Compiler , no marks for guessing that.
Please go through the fol code :

Hope the issue is cleared now.
Ravindra Mohan.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic