— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet351/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   347   348   349   350   351   352   353   354   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

private int x;

 MyInner inner = new MyInner();
public void doStuff() {
inner.go();
}

class MyInner {
void go() {
x = 42;
}

// close inner class

// close outer class
The outer class has a private 
instance variable ‘x’
Make an instance of the 
inner class
The method in the inner class uses the 
outer class instance variable ‘x’, as if ‘x’ 
belonged to the inner class.
If you instantiate an inner class from code within an outer class, the instance 
of the outer class is the one that the inner object will ‘bond’ with. For 
example, if code within a method instantiates the inner class, the inner 
object will bond to the instance whose method is running.
Code in an outer class can instantiate one of its own inner classes, in exactly 
the same way it instantiates any other class... 
new MyInner()
How to make an instance of an inner class
MyOuter
outer
MyInner
inner
int
x
MyOuter
outer
int
x
special 
bond
You can instantiate an inner instance from code running outside the outer class, but you 
have to use a special syntax. Chances are you’ll go through your entire Java life and never 
need to make an inner class from outside, but just in case you’re interested..
.

 class Foo {
public static void main (String[] args) {
MyOuter outerObj = new MyOuter();
MyOuter.MyInner innerObj = outerObj.new MyInner();
}
}
inner class 
instances
Side bar
call a method on the 
inner class



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   347   348   349   350   351   352   353   354   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish