File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner Class" Watch "Inner Class" New topic
Author

Inner Class

Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

this.A1.i = i; //?


SCJP
Visit my download page
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
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


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
Govinda
Greenhorn

Joined: Nov 20, 2000
Posts: 29
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

Joined: Nov 15, 2000
Posts: 59
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).]


Aniruddha
 
GeeCON Prague 2014
 
subject: Inner Class