Java Reference
In-Depth Information
id created
[instance] id.double2 = 1.0
[instance] id.double4 = 10.0
[instance] id.int2 = 1000000000
[instance] id.string2 = abc
Asyoustudythisoutputinconjunctionwiththeaforementioneddiscussionofclass
initializers and instance initializers, you will discover some interesting facts about ini-
tialization:
• Classfieldsinitializetodefaultorexplicitvaluesjustafteraclassisloaded.Im-
mediatelyafteraclassloads,allclassfieldsarezeroedtodefaultvalues.Code
within the <clinit>() method performs explicit initialization.
• All class initialization occurs prior to the <clinit>() method returning.
• Instance fields initialize to default or explicit values during object creation.
When new allocatesmemoryforanobject,itzeroesallinstancefieldstodefault
values. Code within an <init>() method performs explicit initialization.
• All instance initialization occurs prior to the <init>() method returning.
Additionally,becauseinitializationoccursinatop-downmanner,attemptingtoaccess
thecontentsofaclassfieldbeforethatfieldisdeclared,orattemptingtoaccessthecon-
tents of an instance field before that field is declared causes the compiler to report an
illegal forward reference .
Inheriting State and Behaviors
Wetendtocategorizestuffbysayingthingslike“carsarevehicles”or“savingsaccounts
are bank accounts.” By making these statements, we really are saying that cars inherit
vehicular state (e.g., make and color) and behaviors (e.g., park and display mileage),
andthatsavingsaccountsinheritbankaccountstate(e.g.,balance)andbehaviors(e.g.,
depositandwithdraw).Car,vehicle,savingsaccount,andbankaccountareexamplesof
real-worldentitycategories,and inheritance isahierarchicalrelationshipbetweensim-
ilarentitycategoriesinwhichonecategoryinheritsstateandbehaviorsfromatleastone
otherentitycategory.Inheritingfromasinglecategoryiscalled single inheritance ,and
inheriting from at least two categories is called multiple inheritance .
Java supports single inheritance and multiple inheritance to facilitate code re-
use—why reinvent the wheel? Java supports single inheritance in a class context, in
which a class inherits state and behaviors from another class through class extension.
Search WWH ::




Custom Search