Chapter 3 of 'Java™ How to Program' introduces fundamental concepts of classes, objects, methods, and strings in Java. It explains how to declare classes, instantiate objects, and the significance of access modifiers, method headers, and parameters. The chapter also covers instance variables, encapsulation, and the use of set and get methods to manipulate private fields.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
5 views64 pages
Jhtp9 Ch03 B
Chapter 3 of 'Java™ How to Program' introduces fundamental concepts of classes, objects, methods, and strings in Java. It explains how to declare classes, instantiate objects, and the significance of access modifiers, method headers, and parameters. The chapter also covers instance variables, encapsulation, and the use of set and get methods to manipulate private fields.
Education, Inc. All Rights Reserved. 4 3.2 Declaring a Class with a Method and Instantiating an Object of a Class Create a new class (GradeBook) Use it to create an object. Each class declaration that begins with keyword
public must be stored in a file that has the same
name as the class and ends with the .java file-name extension. Keyword public is an access modifier. Indicates that the class is “available to the public”
Education, Inc. All Rights Reserved. 5 3.2 Declaring a Class with a Method and Instantiating an Object of a Class (Cont.) The main method is called automatically by the Java Virtual Machine (JVM) when you execute an application. Normally, you must call methods explicitly to tell them to perform their tasks. A public is “available to the public” It can be called from methods of other classes. The return type specifies the type of data the method returns after performing its task. Return type void indicates that a method will perform a task but will not return (i.e., give back) any information to its calling method when it completes its task.
Education, Inc. All Rights Reserved. 6 3.2 Declaring a Class with a Method and Instantiating an Object of a Class (Cont.) Method name follows the return type. By convention, method names begin with a lowercase first letter and subsequent words in the name begin with a capital letter. Empty parentheses after the method name indicate that the method does not require additional information to perform its task. Together, everything in the first line of the method is typically called the Method header Every method’s body is delimited by left and right braces. The method body contains one or more statements that perform the method’s task.
Education, Inc. All Rights Reserved. 11 3.2 Declaring a Class with a Method and Instantiating an Object of a Class (Cont.) Class instance creation expression Keyword new creates a new object of the class specified to the right of the keyword. Used to initialize a variable of a class type. The parentheses to the right of the class name are required. Parentheses in combination with a class name represent a call to a constructor, which is similar to a method but is used only at the time an object is created to initialize the object’s data.
Education, Inc. All Rights Reserved. 12 3.2 Declaring a Class with a Method and Instantiating an Object of a Class (Cont.) Call a method via the class-type variable Variable name followed by a dot separator (.), the method name and parentheses. Call causes the method to perform its task. Any class can contain a main method. The JVM invokes the main method only in the class used to execute the application. If multiple classes that contain main, then one that is invoked is the one in the class named in the java command.
Education, Inc. All Rights Reserved. 13 3.2 Declaring a Class with a Method and Instantiating an Object of a Class (Cont.) Compiling an Application with Multiple Classes Compile the classes in Fig. 3.1 and Fig. 3.2 before executing. Type the command javac GradeBook.java GradeBookTest.java If the directory containing the application includes only this application’s files, you can compile all the classes in the directory with the command javac *.java
Education, Inc. All Rights Reserved. 14 3.3 Declaring a Class with a Method and Instantiating an Object of a Class (Cont.) Figure 3.3: UML class diagram for class GradeBook. Each class is modeled in a class diagram as a rectangle with three compartments. Top: contains the class name centered horizontally in boldface type. Middle: contains the class’s attributes, which correspond to instance variables (Section 3.5). Bottom: contains the class’s operations, which correspond to methods. Operations are modeled by listing the operation name preceded by an access modifier (in this case +) and followed by a set of parentheses. The plus sign (+) corresponds to the keyword public.
Education, Inc. All Rights Reserved. 17 3.3 Declaring a Method with a Parameter (Cont.) A method can require one or more parameters that represent additional information it needs to perform its task. Defined in a comma-separated parameter list Located in the parentheses that follow the method name Each parameter must specify a type and an identifier. A method call supplies values—called arguments—for each of the method’s parameters.
Education, Inc. All Rights Reserved. 22 3.3 Declaring a Method with a Parameter (Cont.) More on Arguments and Parameters The number of arguments in a method call must match the number of parameters in the parameter list of the method’s declaration. The argument types in the method call must be “consistent with” the types of the corresponding parameters in the method’s declaration.
Education, Inc. All Rights Reserved. 23 3.3 Declaring a Method with a Parameter (Cont.) The UML class diagram of Fig. 3.6 models class GradeBook of Fig. 3.4. The UML models a parameter by listing the parameter
name, followed by a colon and the parameter type in
the parentheses- following the operation name. The UML type String corresponds to the Java type
Education, Inc. All Rights Reserved. 28 3.4 Instance Variables, set Methods and get Methods (Cont.) A class normally consists of one or more methods that manipulate the attributes that belong to a particular object of the class. Attributes are represented as variables in a class declaration. Called fields. Declared inside a class declaration but outside the bodies of the class’s method declarations. Instance variable When each object of a class maintains its own copy of an attribute, the field is an instance variable Each object (instance) of the class has a separate instance of the variable in memory.
Education, Inc. All Rights Reserved. 35 3.4 Instance Variables, set Methods and get Methods (Cont.) Unlike local variables, which are not automatically initialized, every field has a default initial value—a value provided by Java when you do not specify the field’s initial value. Fields are not required to be explicitly initialized before
they are used in a program—unless they must be
initialized to values other than their default values. The default value for a field of type String is null
Education, Inc. All Rights Reserved. 39 3.4 Instance Variables, set Methods and get Methods (Cont.) Figure 3.9 contains an updated UML class diagram for the version of class GradeBook in Fig. 3.7. Models instance variable courseName as an attribute in the middle compartment of the class. The UML represents instance variables as attributes by listing the attribute name, followed by a colon and the attribute type. A minus sign (–) access modifier corresponds to access modifier private.
Education, Inc. All Rights Reserved. 46 3.6 Initializing Objects with Constructors (Cont.) By default, the compiler provides a default constructor with no parameters in any class that does not explicitly include a constructor. Instance variables are initialized to their default values. Can provide your own constructor to specify custom initialization for objects of your class. A constructor’s parameter list specifies the data it requires to perform its task. Constructors cannot return values, so they cannot specify a return type. Normally, constructors are declared public. If you declare any constructors for a class, the Java compiler will not create a default constructor for that class.
Education, Inc. All Rights Reserved. 55 3.7 Floating-Point Numbers and Type double (Cont.) Java treats all floating-point literals (such as 7.33 and 0.0975) as double values by default. Appendix D, Primitive Types shows the ranges of