Chapter5_2-Methods

Passing a message to an object means ordering this

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: age Passing Principle or Method Invocation • Message passing is the principle that allows objects to communicate by exchanging messages. • Passing a message to an object means ordering this latter to execute a specific method. • Passing messages to objects is also known as method invocation. Page 9 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Method Invocation • Invoking a method of a given object requires using: • the instance variable that refers to this object. • the dot (.) operator as following: instanceVariable.methodName(arguments) public class CourseRegistration { public static void main(String args) { Course course1, course2; //Create and assign values to course1 course1 = new Course( ); course1.enterDataFromKeyBoard(); course1.display(); //Create and assign values to course2 course2 = new Course( ); course2.enterDataFromKeyBoard(); course2.display(); } } Page 10 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Method Invocation Execution Schema class Client { class X { public static void main(String arg) { ... X obj = new X(); // Block statement 1 public void method() { obj.method(); // Block statement 2 } // Method body ... } } ... } The client Block statement 1 executes The method Invocation Block statement 2 starts Passing Parameters if exist Return result if any The method body starts The method body finishes The client Page 11 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Returning a Value from a Method • A method returns to the code that invoked it when it: • completes all the statements in the method, • reaches a return statement, or • throws an exception (covered in CSC 113), • If the method returns a value: • The caller...
View Full Document

Ask a homework question - tutors are online