• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Declaration of inner class

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I would like to know whether the following declaration for InnerClass (ic1 to ic4) are valid.
I've compiled and run the code and there's no error. However, the answer for the question are only ic3 and ic4. Can anyone explain to me which is correct? Thanks in advance!
public class OuterClass
{
public static int i;
class InnerClass {}
public void innerClassDemo()
{
InnerClass ic1 = new InnerClass();
InnerClass ic2 = new OuterClass.InnerClass();
InnerClass ic3 = new OuterClass().new InnerClass();
OuterClass.InnerClass ic4 = new OuterClass.InnerClass();
}
}
 
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I Think ic1...ic4 shows ways to create object of inner class and not the decleration innerclass !!!
and in this case all ic1..ic4 are correct beacuse
method innerClassDemo and class InnerCass are in same class so all ways of creating object of innerclass are valid.Had innerclass being defined in other class then first ic1 and ic2 would definately generate complier error.
cheers
Praful
 
Yee Tien Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Praful,
Thank you for replying! Also thanks for pointing out my error I still don't know the terminology well.
I've tried ic3 & ic4 with other class, and I've got the following errors.
Test.java:293: cannot resolve symbol
symbol : class InnerClass
location: class Test
InnerClass ic3 = new OuterClass().new InnerClass();
C:\j2sdk1.4.1\bin>javac Test.java
Test.java:294: not an enclosing class: OuterClass
OuterClass.InnerClass ic4 = new OuterClass.InnerClass();
The only line that works fine and I can think of is: OuterClass.InnerClass ic3 = new OuterClass().new InnerClass();
Could you explain to me why is it so?
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Test.java:293: cannot resolve symbol
symbol : class InnerClass
location: class Test
InnerClass ic3 = new OuterClass().new InnerClass();
I think this error happens because InnerClass is in scope in anotherClass only if anotherClass extends OuterClass! to correct, change the line:
InnerClass ic3 = new OuterClass().new InnerClass();
to
OuterClass.InnerClass ic3 = new OuterClass().new InnerClass();

REMEMBER what Praful Thakare said:
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every inner class has a reference to the object that contains it. Therefore, that object must exist before you can have an inner class. In this line of code:

You never create an object of type OuterClass to contain the instance of the InnerClass. That must be done, like this:

That snippet first creates an instance of the enclosing class and then uses that class to instantiate the inner class.
Take a look at this for all sorts of good information on inner classes.
I hope that helps,
Corey
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ooops!!! forgot to say what he said... Sorry.
He said:"Had innerclass being defined in other class then first ic1 and ic2 would definately generate complier error."

about ic3 you need to place "OuterClass.InnerClass" in the declaration of the object that you want to create if the class in which you want to create this object does not extend OuterClass.
 
Yee Tien Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Thanks for replying!
Got the point for ic3! However for ic4, it's the answer from a mock test. Can I say that the answer is wrong?
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic