Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner Class

 
Santhosh Kumar
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
class A {
int i = 10;

class A1 {
int i = 20;
}
}
class B extends A {
int j = 30;

B(int i, int j) {
//Here I want to initialize to i variable of A1 class. Is this possible?
this.j = j;
}
}

Thanks.
 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this.A1.i = i; //?
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, don't think this.A1.i will work. (At least it doesn't for my version of the JDK. :-))
When you instantiate A you don't automatically get an A1. You need to explicitly create an A1. If A had a ctor then that might be a goob place to do it.
Given the example code, here is a way to set the i var of A1:
// inside class B
B(int i,
int j)
{
A.A1 a1 = new A.A1(); // can also use A1 a1 = new A1
this.j = j;
}
The next question that arises is how to access this A1 instance. It would seem that without A or B having a member variable of class A1 it would go out of scope.
Help, anyone!?
John
 
Govinda
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are absolutely right John.
actully i was typing a detail explaination in this regard
but now you say the same thing which i want to provide.
this is true. inner class variable initialization is absolutely meaningless if explicitly that inner object is not created. what people want to do with that initialization ? i didn't understand. because learning and thinking track is not proper so i expect that in future i might see a question from a "learner" that why java interface can't have constructor ? and when i ans to that question in my way i would definately get the reply that, we are learner and this is our right to stuff this site with these kind of questions.
but because of your very accurate reply is there so i think that i should not post my reply which is same as you but have some more detail explaination.
-Good Day John
Originally posted by John Wetherbie:
No, don't think this.A1.i will work. (At least it doesn't for my version of the JDK. :-))
When you instantiate A you don't automatically get an A1. You need to explicitly create an A1. If A had a ctor then that might be a goob place to do it.
Given the example code, here is a way to set the i var of A1:
// inside class B
B(int i,
int j)
{
A.A1 a1 = new A.A1(); // can also use A1 a1 = new A1
this.j = j;
}
The next question that arises is how to access this A1 instance. It would seem that without A or B having a member variable of class A1 it would go out of scope.
Help, anyone!?
John

 
Aniruddha Mukhopadhyay
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I have modified your code to initialize B with value of i variable of inner class A1. Since B extends A, it also has inner class A1. Compile and run it with "java B" to see that initialization is OK.

class B extends A {
int j = 30;
int i;
B(int i, int j) {
i = (new A1()).i;
this.j = j;
System.out.println("output of A1()).i " + i);
}
public static void main (String args[]){
B b = new B(0,0);
}
}
Aniruddha
[This message has been edited by aniruddha mukhopadhyay (edited November 27, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic