— Scott McNealy, Sun Microsystems Chairman, President and ceo


//returns the lesser of a and b



Download 40,57 Mb.
Pdf ko'rish
bet263/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   259   260   261   262   263   264   265   266   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

//returns the lesser of a and b
}
Math
min()
max()
abs()
...
Song
title
play()
regular (non-static) method
static method
The current value of the ‘title’ 
instance variable is the song that 
plays when you call play().
No instance variables. 
The method behavior 
doesn’t change with 
instance variable state.
Math.min(42,36);
NO OBJECTS!!
Absolutely NO OBJECTS 
anywhere in this picture
!
Use the Class name, rather 
than a reference variable 
name.
two instances 
of class Song
Instance variable value affects 
the behavior of the play() 
method.
Song o
bj
ec



static 
methods
276
 
chapter 10
Math.min(88,86);
Call a static method using a 
class
name
Math
min()
max()
abs()
...
Call a non-static method using a 
reference variable
name
t2
Song t2 = new Song();
t2.play();
What it means to have a 
class with static methods.
Often (although not always), a class with static 
methods is not meant to be instantiated. In Chapter 
8 we talked about abstract classes, and how marking 
a class with the 
abstract 
modifi er makes it 
impossible for anyone to say ‘new’ on that class type. 
In other words, it’s impossible to instantiate an abstract 
class.
But you can restrict other code from instantiating 
non-abstract class by marking the constructor 
private
. Remember, a method marked private means 
that only code from within the class can invoke 
the method. A constructor marked private means 
essentially the same thing—only code from within 
the class can invoke the constructor. Nobody can 
say ‘new’ from outside the class. That’s how it works 
with the Math class, for example. The constructor 
is private, you cannot make a new instance of Math. 
The compiler knows that your code doesn’t have 
access to that private constructor.
This does not mean that a class with one or more 
static methods should never be instantiated. In fact, 
every class you put a main() method in is a class with 
a static method in it!
Typically, you make a main() method so that you 
can launch or test another class, nearly always by 
instantiating a class in main, and then invoking a 
method on that new instance.
So you’re free to combine static and non-static 
methods in a class, although even a single non-static 
method means there must be some way to make an 
instance of the class. The only ways to get a new 
object are through ‘new’ or deserialization (or 
something called the Java Refl ection API that we 
don’t go into). No other way. But exactly who says new 
can be an interesting question, and one we’ll look at 
a little later in this chapter.



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   259   260   261   262   263   264   265   266   ...   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