Chapter5_2-Methods

receivingvaraiable instancevariablemethodname page

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ableName return variableName; } } public class ClientClass { public static void main (String args) { ClassName instanceVariable = new ClassName(); returnType receivingVaraiable; ... receivingVaraiable = instanceVariable.methodName(…); ... } } Page 15 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Passing Information to a Method • The declaration for a method declares the number and the type of the data-items to be passed for that method. • Parameters refers to the list of variables in a method declaration. • Arguments are the actual values that are passed in when the method is invoked. • When you invoke a method, the arguments used must match the declaration's parameters in type and order Page 16 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Arguments and Parameters • An argument is a value we pass to a method. • A parameter is a placeholder in the called method to hold the value of the passed argument. class Sample { class Account { public static void main(String arg) { parameter ... Account acct = new Account(); ... public void add(double amt) { acct.add(400); ... } } ... } Page 17 balance = balance + amt; ... } argument Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Matching Arguments and Parameters The number or arguments and the parameters must be the same • Arguments and parameters are paired left to right • The matched pair must be assignmentcompatible (e.g. you cannot pass a double argument to a int parameter) • Page 18 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Parameter Passing • When a method is called: • The parameters are created. • The values of arguments are copied into the parameters’ variables. • The variables...
View Full Document

Ask a homework question - tutors are online