This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3201
    
    2
You sure can...
this.j = new A1().i;
HTH,
Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

[This message has been edited by Paul Anil (edited November 27, 2000).]


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Hi Paul,
Thanks for the reply. But we wanted to know how to assign some value to variable i in inner class A1.
Santhosh.
manikandan renganathan
Greenhorn

Joined: Nov 26, 2000
Posts: 2
Hi Santosh,
The Inner class is known only within the scope of outer class.
So, you should have another method at your class A to assign the parameter value to the inner class variable.
Like this,
class a
{
int i;

class a1
{
int i=20;
}

void assign(int l)
{
a1 a1o = new a1();
a1o.i=l;
}

}
class b extends a
{
int j = 30;

b(int i, int j)
{
super.i=i;
super.assign(i);
this.j=j;
}
}
bye
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3201
    
    2
As your inner class A1 is not private, it is inherited by class B. All you need to do to set i is:
new A1().i = 10; //or whatever.
(Also, if you can do this.j = new A1().i (As I showed before) You can as well do new A1().i = 10; !!!
HTH,
-Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

[This message has been edited by Paul Anil (edited November 27, 2000).]
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Thanks a lot.
sino
Greenhorn

Joined: Nov 29, 2000
Posts: 5
what about if there is no inheritance between class A and B, and I still want to do the same thing in B??
I have tried:
A a = new A();
A.A1 a1= a.new A.A1;
but doesn't work!!!
Galen Cheng
Greenhorn

Joined: Dec 15, 2000
Posts: 1
hello
You can create the inner class as this :
A.A1 a1= new A(). new A1();
 
 
subject: Inner Class
 
Similar Threads
Inner Class
Declaration before read Rule in Initializers
Doubt in Exception Handle and Declare
inner class member initialization
garbagecollection