JAVA Chanper3

¡Supera tus tareas y exámenes ahora con Quizwiz!

Declaring Objects and Using Their Methods

Declaring a class does not create any actual objects To create an instance of a class: Supply a type and an identifier Allocate computer memory for the object Use the new operator Employee someEmployee; someEmployee = new Employee(); or Employee someEmployee = new Employee();

Return Typev

Describes the type of data the method sends back to the calling method If no data is returned to the method, the return value is void

A constructor method:

Must have the same name as the class it constructs Cannot have a return type public access modifier

Private access for fields

No other classes can access the field's values. Only methods of the same class are allowed to use private variables

Instance variables

Nonstatic fields given to each object

Place the entire method within the class that will use it

Not within any other method

The default constructor provides specific initial values to an object's data fields

Numeric fields Set to 0 (zero) Character fields Set to Unicode '\u0000' Boolean fields Set to false Nonprimitive object fields Set to null

Classes you create become data types

Often referred to as abstract data types (ADTs) Implementation is hidden and accessed through public methods Programmer-defined data type Not built into the language

The method header contains:

Optional access specifiers. A return type. An identifier. Parentheses. Might contain data to be sent to the method.

Define the following:

Optional access specifiers. Return type for the method. Method name. Parameter type. Local name for the parameter.

Declare an object from one of your classes

Provide the type and identifier

Default constructors

Require no arguments Created automatically by a Java compiler For any class Whenever you do not write a constructor

Reference to the object

The name for a memory address where the object is held

Interface 接口

The only part of a method that the client sees or with which it interacts

Extended

To be used as a basis for any other class

Data fields

Variables declared within a class but outside of any method

Calling method (client method)

Makes a method call

A method must include:

1Method header: Also called a declaration. 2Method body: Between a pair of curly braces, Contains the statements that carry out the work Also called implementation.

? Classes contain methods

1Mutator methods :Set or change field values. 2Accessor methods:Retrieve values. 3Nonstatic methods. 4Instance methods: "Belong" to objects, Typically declare nonstatic data fields, static class variables are not instance variables.

The return type can be any type used in Java

1Primitive types 2Class types 3void: Returns nothing

Creating a Method That Requires Multiple Parameters

A method can require more than one parameter, List the arguments within the call to the method , Separate with commas, Call a method. Arguments sent to the method must match the parameters listed in the method declaration by: Number , Type.

Constructor method

A method that creates and initializes class objects. You can write your own constructor methods. Java writes a constructor when you don't write one. The name of the constructor is always the same as the name of the class whose objects it constructs.

Method's type

A method's return type

Method

A program module Contains a series of statements Carries out a task

public class

Accessible by all objects

Create a class header with three parts:

An optional access modifier The keyword class Any legal identifier for the name of the class

Chaining Method Calls

Any method might call any number of other methods Method acts as a black box Do not need to know how it works Just call and use the result

Place data fields in logical order

At the beginning of a class List the fields vertically

Method Name

Can be any legal identifier , Must be one word, No embedded spaces, Cannot be a Java keyword.

Access Specifiers

Can be public, private, protected , or package. public access: allows use by any other class. Also called access modifiers, Methods most commonly use public access.

return statement

Causes a value to be sent from the called method back to the calling method

Methods are often called upon to return a piece of information to the source of the request

Class client or class user: An application or a class that instantiates objects of another prewritten class. Assign a name to the class: Determine what data and methods will be part of the class.

Method A series of statements that carry out a task A declaration includes the parameter type and local name for a parameter You can pass multiple arguments to methods Has a return type Class objects Have attributes and methods associated with them Instantiate objects that are members of a class

Constructor A method establishes an object and provides specific initial values for an object's data fields Everything is an object Every object is a member of a more general class Implementation hiding, or encapsulation private data fields public access methods

get method

Controls how a value is retrieved

set method

Controls the data values used to set a variable

Data hiding using encapsulation

Data fields are usually private The client application accesses them only through public interfaces

Parameters

Data items received by the method

Arguments

Data items you use in a call to a method

Creating a static Method that Requires No Arguments and Returns No Values Creating static Methods that Accept Arguments and Return a Value Creating a Class that Contains Instance Fields and Methods Declaring and Using Objects Adding a Constructor to a Class Understanding that Classes are Data Types

Don't place a semicolon at the end of a method header Don't think "default constructor" means only the automatically supplied constructor Don't think that a class's methods must: Accept its own fields' values as parameters Return values to its own fields Don't create a class method that has a parameter with the same identifier as a class field

Implementation hiding

Encapsulation of method details within a class The calling method needs to understand only the interface to the called method

Parentheses

Every method header contains a set of parentheses that follow the identifier, May contain data to be sent to the method, Fully qualified identifier, A complete name that includes the class,

Classes and Objects

Every object is a member of a class. Is-a relationships. An object "is a" concrete example of the class. The zoo's shark "is a" Fish. Instantiation. Shark is an instantiation of the Fish class. Reusability.

Execute a method

Invoke or call from another method

Called method

Invoked(调用) by a calling method

Data fields and methods may be placed in any order within a class

It's common to list all data fields first Names and data types can be seen before reading the methods that use the data fields

Local variable

Known only within the boundaries of the method, Each time the method executes: The variable is redeclared, A new memory location large enough to hold the type is set up and named,

Unreachable statements (dead code)

Logical flow leaves the method at the return statement Can never execute Causes a compiler error


Conjuntos de estudio relacionados

Advanced Heath Assessment and Differential Diagnosis Module 2

View Set

Network+ Guide to Networks Seventh Edition Chapter 8 Key Terms & Review Questions

View Set

Personnel Readiness Transformation Training

View Set

Ch 34: Liability to Third Parties & Termination

View Set