Download presentation
Presentation is loading. Please wait.
Published byΟινώνη Αλιβιζάτος Modified over 6 years ago
1
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows you to do these manipulations. A method has two options. It may simply perform a service of some kind, or it may compute a value and give the answer back. The action of giving a value back is called returning a value. Methods that simply perform some action are called void methods. Methods that return a value are called return, or non-void, methods. Return method must define what data type they will be returning and include a return statement in the method. A return method can return any kind of data type. Using the word public in the method declaration ensures that the method is available for other objects from other classes to use.
2
Method Declarations A method declaration specifies the code that will be executed when the method is invoked (or called) When a method is invoked, the flow of control jumps to the method and executes its code When complete, the flow returns to the place where the method was called and continues The invocation may or may not return a value, depending on how the method is defined
3
Method Control Flow The called method can be within the same class, in which case only the method name is needed myMethod(); myMethod compute
4
Method Control Flow The called method can be part of another class or object obj.doIt(); main doIt helpMe helpMe();
5
Method Header A method declaration begins with a method header
public char calc (int num1, int num2, String message) method name parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal argument return type Methods are declared using a line of code called a method declaration statement. The declaration includes the access modifier, the return type, the name of the method and the parameter list. The method signature only includes the name of the method and the parameter list.
6
Method Body The method header is followed by the method body
public char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } sum and result are local data They are created each time the method is called, and are destroyed when it finishes executing The return expression must be consistent with the return type
7
The return Statement The return type of a method indicates the type of value that the method sends back to the calling location A method that does not return a value has a void return type A return statement specifies the value that will be returned return expression; Its expression must conform to the return type public char calc (int num1, int num2, String message) The return type in the method header can e a primitive type, class name, or the reserved word void. Then a method does not return any value, void is used is the return type, as is always done with the main method. A method that returns a value must have a return statement. After a return statement is executed, control is immediately returned to the statement in the calling method, and processing continues there. A method that does not return a value does not usually need a return statement because it automatically returns to the calling method when it is done. A method with a void return type may, however, have a return statement with an expression.
8
Parameters Each time a method is called, the actual parameters in the invocation are copied into the formal parameters ch = obj.calc (25, count, "Hello"); public char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } Values that are passed to method or constructor are called actual parameters or arguments, while the variables in the parameter list f the method declaration are called formal parameters. When an actual parameter is of a primitive type its value cannot be changed in the method because on the value of the variable is sent to the method. The formal parameters are identifiers that act as variables inside the method. Their initial values come from the actual parameters in the invocation (the call to the method). When a method is called, the value in each actual parameter is copied and stored in the matching formal parameter. Actual parameters can be literals, variables, or full expressions. If an expression is used as an actual parameter, it is fully evaluated before the method call and the result is passed as the parameter. To put it another way, the formal parameter variable becomes a copy of the actual parameter. He method cannot alter the value of the actual parameter variables. The formal parameter variables only receive the value of the actual parameter. The method only has the ability to change the value of the formal parameter variable, not the value of the actual parameter.
9
Preconditions and Postconditions
A precondition is a condition that should be true when a method is called A postcondition is a condition that should be true when a method finishes executing These conditions are expressed in comments above the method header Both preconditions and postconditions are a kind of assertion, a logical statement that can be true or false which represents a programmer´s assumptions about a program You know what happens when you assume, right? Well, a precondition states what you are allowed to assume about a parameter. It’s a statement in the documentation comment before a constructor or method signature that describes the expectations of the parameter sent to the method. On the AP test, reading the precondition can help you understand the nature of the parameters. A postcondition is what is expected of the method after it is executed. It is what is true when a method finishes executing. If a method’s precondition is violated, the postcondition does not apply.
10
Constructors Revisited
Recall that a constructor is a special method that is used to initialize a newly created object When writing a constructor, remember that: it has the same name as the class it does not return a value it has no return type, not even void it typically sets the initial values of instance variables The programmer does not have to define a constructor for a class Constructors are the builders of the virtual object from a class. They are used in combination with the keyword new to create an object from the class. Constructors have the same name as the class and are typically listed near the top of the class. When you define a new class, it comes with a no-argument constructor, kind of like a default constructor. The no-argument constructor gets it name because no information is passed to this constructor when creating a new object. It is the constructor that allows you to make a generic object from a class. If you will know some information about an object prior to creating it then you may want to write a parameterized constructor in your class. You use the parameterized constructor to give initial values to the instance variables when creating a brand-new object. When you want to make an object from a class, you have to call one of the classes constructors. However, you may have more than one constructor, including the no-argument constructor or one or more parameterized constructors. The computer decides which constructor to call by looking at the parameter list in the declaration of each constructor. Whichever constructor matches the call to the constructor is sued to create the object.
11
Local Data Local variables can be declared inside a method
The formal parameters of a method create automatic local variables when the method is invoked When the method finishes, all local variables are destroyed (including the formal parameters) Keep in mind that instance variables, declared at the class level, exists as long as the object exists Any method in the class can refer to instance data
12
Accessors and Mutators
Since instance data usually has private visibility, it can only be accessed through methods An accessor method provides read-only access to a particular value A mutator method changes a particular value For a data value X, accessor and mutator methods are usually named getX and setX Methods that return the value of an instance variable are called accessor (or getter) methods. Methods that change the value of an instance variable are called mutator (or modifier or setter) methods. Methods that don’t perform either one of these duties are simply called methods.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.