CS 18000 Lecture Notes - Lecture 16: Reserved Word

20 views5 pages
Methods and Classes
Java Class
Fields: Attributes of an object or the class
Methods: Operations on an object or the class
Within certain limitations:
Fields are accessible to methods of the class
Fields and methods may be static or non-static
Static and Non-Static
Static field:
One memory location shared by all objects of the class
Same value shared by all methods
Counter
Create class w/ static and non-static counters
Methods
Parameterized block of code that may return a value
Every method exists inside some class
Useful for
Reusability
Readability
Modularity
Basic Method Syntax
return_type methodName(param_list) {
statements;
return_if_needed;
}
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in
Parameters and Arguments
Parameters allow a method to work on different data values
Parameter: Variable local to a method
Argument: Expression that is passed to the corresponding parameter at time of method
call
Argument values to a method must match the number and types of parameters of method
Argument values are copied into parameter variables
int calc (double x, int y) {.. return z;}
Flow of Control for Method Calls
Before call: Argument values at calling site are copied to parameter variables in called
method
Then …
Calling method is “suspended”
Called method begins at the top of the method body and runs to completion
Calling method continues where it left off
After call: Return value from called method becomes value returned to calling site
Parameters: Call by Value
Parameter variables are distinct from variables passed in as arguments
void changer(int x) {
x = 12;
}
int y = 5;
changer(y);
System.out.println(y);
Static and Non-Static Methods
Static method:
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Fields: attributes of an object or the class. Methods: operations on an object or the class. Fields are accessible to methods of the class. Fields and methods may be static or non-static. One memory location shared by all objects of the class. Parameterized block of code that may return a value. Basic method syntax return_type methodname(param_list) { statements; return_if_needed; Parameters allow a method to work on different data values. Argument: expression that is passed to the corresponding parameter at time of method call. Argument values to a method must match the number and types of parameters of method. Argument values are copied into parameter variables int calc (double x, int y) { return z;} Before call: argument values at calling site are copied to parameter variables in called method. Called method begins at the top of the method body and runs to completion. After call: return value from called method becomes value returned to calling site.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents