[ Pobierz całość w formacie PDF ]
.A data memberis an object of any type that you can communicate with via its reference.Itcan also be one of the primitive types (which isn t a reference).If it is areference to an object, you must initialize that reference to connect it to anactual object (using new, as seen earlier) in a special function called aconstructor (described fully in Chapter 4).If it is a primitive type you caninitialize it directly at the point of definition in the class.(As you ll seelater, references can also be initialized at the point of definition.)110 Thinking in Java www.BruceEckel.comEach object keeps its own storage for its data members; the data membersare not shared among objects.Here is an example of a class with somedata members:class DataOnly {int i;float f;boolean b;}This class doesn t do anything, but you can create an object:DataOnly d = new DataOnly();You can assign values to the data members, but you must first know howto refer to a member of an object.This is accomplished by stating thename of the object reference, followed by a period (dot), followed by thename of the member inside the object:objectReference.memberFor example:d.i = 47;d.f = 1.1f;d.b = false;It is also possible that your object might contain other objects that containdata you d like to modify.For this, you just keep connecting the dots.For example:myPlane.leftTank.capacity = 100;The DataOnly class cannot do much of anything except hold data,because it has no member functions (methods).To understand how thosework, you must first understand arguments and return values, which willbe described shortly.Default values for primitive membersWhen a primitive data type is a member of a class, it is guaranteed to get adefault value if you do not initialize it:Chapter 2: Everything is an Object 111Primitive type Defaultboolean falsechar \u0000 (null)byte (byte)0short (short)0int 0long 0Lfloat 0.0fdouble 0.0dNote carefully that the default values are what Java guarantees when thevariable is used as a member of a class.This ensures that membervariables of primitive types will always be initialized (something C++doesn t do), reducing a source of bugs.However, this initial value may notbe correct or even legal for the program you are writing.It s best to alwaysexplicitly initialize your variables.This guarantee doesn t apply to local variables those that are not fieldsof a class.Thus, if within a function definition you have:int x;Then x will get some arbitrary value (as in C and C++); it will notautomatically be initialized to zero.You are responsible for assigning anappropriate value before you use x.If you forget, Java definitely improveson C++: you get a compile-time error telling you the variable might nothave been initialized.(Many C++ compilers will warn you aboutuninitialized variables, but in Java these are errors.)Methods, arguments,and return valuesUp until now, the term function has been used to describe a namedsubroutine.The term that is more commonly used in Java is method, as in a way to do something. If you want, you can continue thinking in terms112 Thinking in Java www.BruceEckel.comof functions.It s really only a syntactic difference, but from now on method will be used in this book rather than function.Methods in Java determine the messages an object can receive.In thissection you will learn how simple it is to define a method.The fundamental parts of a method are the name, the arguments, thereturn type, and the body.Here is the basic form:returnType methodName( /* argument list */ ) {/* Method body */}The return type is the type of the value that pops out of the method afteryou call it.The argument list gives the types and names for theinformation you want to pass into the method.The method name andargument list together uniquely identify the method.Methods in Java can be created only as part of a class
[ Pobierz całość w formacie PDF ]