190 likes | 383 Views
Method Definition. public void MethodName() // Method Header { // Start of method body } // End of method body. Example. The Method Header modifier opt ResultType MethodName (Formal ParameterList )
E N D
Method Definition public void MethodName() // Method Header { // Start of method body } // End of method body • Example • The Method Header modifieropt ResultType MethodName (Formal ParameterList ) public static void main (String argv[ ] ) public void deposit (double amount) public double calculateArea ( )
Method Header • A method declaration begins with a method header int add (int num1, int num2) method name Formal 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 parameter return type
Method Body • The method header is followed by the method body int add (int num1, int num2) { int sum = num1 + num2; return sum; } sum is local data Local data 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
User-Defined Methods • Methods can return zero or one value • Value-returning methods • Methods that have a return type • Void methods • Methods that do not have a return type
calculateArea Method. public double calculateArea() { double area; area = length * width; return area; }
Return statement • Value-returning method uses a return statement to return its value; it passes a value outside the method. • Syntax:return statement return expr; • Where expr can be: • Variable, constant value or expression
User-Defined Methods • Methods can have zero or >= 1 parameters • No parameters • Nothing inside bracket in method header • 1 or more parameters • List the paramater/s inside bracket
Method Parameters- as input/s to a method public class Rectangle { . . . public void setWidth(double w) {width = w;} public void setLength(double l) {length = l;} . . . }
Syntax: Formal Parameter List (dataType identifier, dataType identifier....) Note: it can be one or more dataType Eg. setWidth( double w ) int add (int num1, int num2)
Using Rectangle Instances • We use a method call to ask each object to tell us its area: System.out.println("rectangle1 area " + rectangle1.calculateArea()); System.out.println("rectangle2 area " + rectangle2.calculateArea()); References to objects Method calls rectangle1 area 300 rectangle2 area 500 Printed output:
The RectangleUser Class Definition Class Definition public class RectangleUser { public static void main(String argv[]) { Rectangle rectangle1 = new Rectangle(30,10); Rectangle rectangle2 = new Rectangle(25,20); System.out.println("rectangle1 area " + rectangle1.calculateArea()); System.out.println("rectangle2 area " + rectangle2.calculateArea()); } // main() } // RectangleUser An application must have a main() method Object Creation Object Use
Method Call • Syntax to call a method methodName(actual parameter list); Eg. segi4.setWidth(20.5); obj.add (25, count);
int add (int num1, int num2) { int sum = num1 + num2; return sum; } Formal vs Actual Parameters • When a method is called, the actual parameters in the invocation are copied into the formal parameters in the method header total = obj.add(25, count);
Method Overloading • In Java, within a class, several methods can have the same name. We called method overloading • Two methods are said to have different formal parameter lists: • If both methods have a different number of formal parameters • If the number of formal parameters is the same in both methods, the data type of the formal parameters in the order we list must differ in at least one position
Method Overloading • Example: publicvoid methodABC() publicvoid methodABC(int x) publicvoid methodABC(int x, double y) publicvoid methodABC(double x, int y) publicvoid methodABC(char x, double y) publicvoid methodABC(String x,int y)
Java code for overloading • public class Exam • { • public static void main (String [] args) • { • int test1=75, test2=68, total_test1, total_test2; • Exam midsem=new Exam(); • total_test1 = midsem.result(test1); • System.out.println("Total test 1 : "+ total_test1); • total_test2 = midsem.result(test1,test2); • System.out.println("Total test 2 : "+ total_test2); • } • int result (int i) • { • return i++; • } • int result (int i, int j) • { • return ++i + j; • } • }
Java code (constructor overloading) publicclass Student { String name; int age; Student(String n, int a) { name = n; age = a; System.out.println ("Name1 :" + name); System.out.println ("Age1 :" + age); } Student(String n) { name = n; age = 18; System.out.println ("Name2 :" + name); System.out.println ("Age2 :" + age); } publicstaticvoid main (String args[]) { Student myStudent1=new Student("Adam",22); Student myStudent2=new Student("Adlin"); } }
Object Methods & Class Methods • Object/Instance methods belong to objects and can only be applied after the objects are created. • They called by the following : objectName.methodName(); • Class can have its own methods known as class methods or static methods
Static Methods • Java supports static methods as well as static variables. • Static Method:- • Belongs to class (NOT to objects created from the class) • Can be called without creating an object/instance of the class • To define a static method, put the modifier static in the method declaration: • Static methods are called by : ClassName.methodName();