• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nested class problem  RSS feed

 
Mohammad Nizam Uddin
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
This is a nested class code. I want to create an object of Nested class B. But there is a compiler error when I try to create class B type object ob2. If it is not possible to create an object of nested class type, then why we use Nested class? If someone can provide some explanation with code, it will be of great help.
Thanks.

 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, let's format that code a little better:



Now, class B only exists in an instance of class A. It has no existence outside, so the type will be:


But here's where it gets tricky. You can't say new B() because there is no instance of A. So you have to say:


Now the code works.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute has explained how to solve the problem. I just want to comment that when you write inner classes (as opposed to static nested classes), it's generally easier to let the enclosing classes make new instances:

 
Mohammad Nizam Uddin
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome. Thanks a lot.
 
Mohammad Nizam Uddin
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:First, let's format that code a little better:



Now, class B only exists in an instance of class A. It has no existence outside, so the type will be:


But here's where it gets tricky. You can't say new B() because there is no instance of A. So you have to say:


Now the code works.


Thanks for the solution, but still there is one problem. The output result of sum() is 30. But it should be 10+20+30=60.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because you didn't set i and j on the enclosing instance of the new B.

Use B ob2 = ob1.new B();, instead of ob2 = new A().new B();.
 
Mohammad Nizam Uddin
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Probably because you didn't set i and j on the enclosing instance of the new B.

Use B ob2 = ob1.new B();, instead of ob2 = new A().new B();.


Can you write the whole code please, because its not working.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should show us what you have tried instead, and explain exactly what isn't working: ItDoesntWorkIsUseless.
 
Mohammad Nizam Uddin
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:You should show us what you have tried instead, and explain exactly what isn't working: ItDoesntWorkIsUseless.

The code that Mr. Knute shortum provided works fine. He explained everything with code.
You said to use this B ob2 = ob1.new B(); but you didn't provide the whole code. So I don't understand how to use this. Please kindly give me the whole code so that I may understand.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the real problem is poor design of class A. You have two fields but you have not insisted they be initialised. The way you insist they be initialised is by writing constructors which initialise them. Then there will be no default constructor and no 0 values anywhere.
Add one constructor and only one constructor to class A which does that and when the code compiles, then you will get 60.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code that Mr. Knute shortum provided works fine. He explained everything with code.


I may have set a bad precedent. In general, we like to have people discover their own solutions. I provided code because discovering how to instantiate an inner class is difficult. But you should still try to find a solution on your own first.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!