Download presentation
Presentation is loading. Please wait.
Published byBrent Lester Modified over 9 years ago
1
Java Classes Chapter 1
2
2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining a Java Class Passing Arguments Constructors The toString Method Static Fields and Methods Packages – The Java Class Library
3
3 Objects An object is a program construct that Contains data Performs certain actions The actions are called methods The actions interact to form the solution to a given problem
4
4 Classes A class is a type or kind of object Objects of the same class have The same kinds of data The same methods A class definition is a general description of What the object is What it can do
5
5 An Example of a Class Fig. 1-1 An outline of a class
6
6 Instantiation of the Class Fig. 1-1 Three instances of the class Automobile
7
7 Using Methods in a Java Class Given: jName joe = new Name(); The new operator creates an instance of the class Invokes the constructor method Valued methods return a single value void methods do not return a value
8
8 Using Methods in a Java Class Fig. 1-2 A variable that references an object.
9
9 References and Aliases Primitive types: byte, short, int, long float, double, char, boolean All other types are reference or class types String greeting = "Howdy"; greeting is a reference variable When two variables reference the same instance, they are considered aliases
10
10 References and Aliases Fig. 1-3 Aliases of an object
11
11 Arguments and Parameters Given Name joe = new Name(); joe.setFirst ("Joseph"); joe.setLast ("Brown"); "Joseph" and "Brown" are arguments sent to the methods Invocation of method must have same number of arguments as there are formal parameters in the declaration
12
12 Defining a Java Class Given These are the data fields (instance variables) Note they are private They will require accessor and mutator methods public class Name {private String first; // first name private String last; // last name } // end Name
13
13 Method Definitions Given This is a valued method Returns a String Given This is a void method public void setFirst(String firstName) {first = firstName; } // end setFirst public String getFirst() {return first; }// end getFirst
14
14 Naming Methods Start method name with lowercase letter Use verb or action phrase Start class name with uppercase Use noun or descriptive phrase Local variables A variable declared within a method
15
15 Passing Arguments Call by value For primitive type, parameter initialized to value of argument in call Call by reference For a class type, formal parameter is initialized to the address of the object in the call
16
16 Passing Arguments Fig.1-4 a & b The method giveLastNameTo modifies the object passed to it as an argument.
17
17 Passing Arguments Fig.1-4 c & d The method giveLastNameTo modifies the object passed to it as an argument.
18
18 Passing Arguments Figure 1-5 a & b A method cannot replace an object passed to it as an argument.
19
19 Passing Arguments Figure 1-5 c & d A method cannot replace an object passed to it as an argument.
20
20 Constructors A method that Allocates memory for the object Initializes the data fields Properties Same name as the class No return type, not even void Any number of formal parameters
21
21 Constructors Fig. 1-6 An object (a) after its initial creation; (b) after its reference is lost
22
22 Static Fields and Methods A data field that does not belong to any one object One instance of that data item exists to be shared by all the instances of the class Also called: static field, static variable, class variable
23
23 Static Fields and Methods Fig. 1-7 A static PI versus a non static field
24
24 Packages Multiple related can be conveniently grouped into a package Begin each file that contains a class within the package package myStuff; Place all files within a directory Give folder same name as the package To use the package, begin the program with import myStuff.*;
25
25 The Java Class Library Many useful classes have already been declared Collection exists in Java Class Library Example The class Math is part of the package java.lang
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.