Docsity

Java Methods: Signature, Variables, Calling, Data Structures, Members, Statics, Blocks, Cl, Exams of Computer Science

Partial preview of the text

Download Java Methods: Signature, Variables, Calling, Data Structures, Members, Statics, Blocks, Cl and more Exams Computer Science in PDF only on Docsity!

cs402 - Spring 2003

Introduction to Java

Handout-1d

cs402 - Spring 2003

Methods (i)

•^

Method

is the OOP name for function

– Must be declared always within a class

•^

You can several methods with the same name inthe class– Same-name methods are said to

overload

the name

– Methods with same name should do the same thing

Method “signature”

optAccessQualifier

returnType

methodName

(^

optArgumentList

)^

optThrowsClause

{

…^ //

statements }

cs402 - Spring 2003

Methods (iii)

• Calling a method in the same class class Fruit {int grams;int totalCalories() {

… // Statements} void someMethod() {int i = totalCalories();

//same as i =

this

.totalCalories()

}

cs402 - Spring 2003

Methods (v)

• Calling a method in a different class class Cooking {int grams;Fruit apple = …void otherMethod() {

int i = apple.totalCalories(); // tell what object to use }

cs402 - Spring 2003

Methods (vii)

• Dynamic data structures Ex: class

BinaryTree

private

Object

nodeData;

private

Tree

left;

left

sub-tree

private

Tree

right;

right

sub-tree

cs402 - Spring 2003

Methods (viii)

•^

Per-instance and per-class members^ –^

static

keyword makes something exist per-class, not

per instance of that class

•^

There are four varieties of static– Data; the data belongs to the class, not individual

instances of the class

– Methods; these are methods that belong to the class– Blocks; these are blocks that are executed only once– Classes; these are classes that are nested in another

class

cs402 - Spring 2003

Methods – static data

•^

Inside the class static data is accessed by its name.

•^

Outside the class, static data can be accessed by:– Prefix it with the name of an object of that class OR– Prefix it with the name of the class

Ex:Employee

newhire

=^

new

Employee();

newhire.total_employees=1;

reference

through

the

instance

Employee.total_employees=1;

reference

through

the

class

cs402 - Spring 2003

Methods – static methods

• Static methods, aka

class methods

do class

wide operations and do not apply toindividual objects

Ex:class

Employee

String

name; int

salary; int

employee_id; static

int

total_employees;

static

void

clear()

total_employees

=^

cs402 - Spring 2003

Methods – static method pitfalls• Common error: reference per-object datafrom a static method Ex: public

static

void main(String[] args) {

salary = 50000;

// Compiler error

Employee e

= new Employee();

e.salary =

50000; // This is ok

cs402 - Spring 2003

Methods – static blocks

•^

Block of code: statements within a pair of curlybraces

•^

Static block is prefixed by

static

– Inside a class– Outside all methods– Most commonly used for initialization– Each static block is executed once only, when class if

first loaded into the JVM

– Can only access static data

cs402 - Spring 2003

Methods – static classes

• Nested static class: the declaration of an

entire class (methods, data fields,constructors) as a static member of anotherclass– Nested purely for convenience

cs402 - Spring 2003

Modifiers -

final

• Makes something constant. Can be applied

to code and data– When reference variable is declared final you

can’t make that variable point at some otherobject

– The reference is final not the referenced object

cs402 - Spring 2003

Modifiers –

final

• Method arguments can be marked as final Ex: void someMethod(

final

MyClass c) {

c.field = 7; // Okc = new MyClass(); //compilation error }

cs402 - Spring 2003

Modifiers -

final

•^

Blank final variable

: a final variable of any

kind that does not have an initializer– Must be assigned a value; that value can be

assigned only once

– If you give a value to a blank final in a

constructor, then you must assign it a value ineach constructor