Java Methods - PDF
Document Details
Uploaded by HearteningLiberty5038
College of Engineering and Information Technology
Tags
Summary
These notes provide an overview of Java methods, covering concepts like method creation, calls, arguments, return types, and overloading. It includes examples to illustrate the usage of methods, and focuses on practical implementation in Java programming.
Full Transcript
Java Methods Objectives What is Method? How to create and call a methods. Arguments/parameters Return keyword Overloading methods What is methods? methods are blocks of code that perform specific tasks and are part of a class. They allow code reusability and modul...
Java Methods Objectives What is Method? How to create and call a methods. Arguments/parameters Return keyword Overloading methods What is methods? methods are blocks of code that perform specific tasks and are part of a class. They allow code reusability and modularity by defining a single block of code that can be executed multiple times by "calling" the method. they are used to divide and sort Functionalities within in a class so that the code will readable even if its long. What is methods? What is methods? Creating methods Access Modifier defines the visibility of the method (public, private, protected, no modifier(Default)). Return Type: The data type of the value the method returns. Use void if the method does not return a value. Method Name: Name of the Java method. Used to call the method. Parameters variable passed to the method for processing. Creating methods Access Modifier defines the visibility of the method (public, private, protected, no modifier(Default)). Return Type: The data type of the value the method returns. Use void if the method does not return a value. Method Name: Name of the Java method. Used to call the method. Parameters variable passed to the method for processing. Creating methods Access Modifier defines the visibility of the method (public, private, protected, no modifier(Default)). Return Type: The data type of the value the method returns. Use void if the method does not return a value. Method Name: Name of the Java method. Used to call the method. Parameters variable passed to the method for processing. Parameter is optional Syntax Methods accessModifier ReturnType MethodName(){ //do anything here } Syntax Methods When creating a methods, make sure it must be inside the class and outside of other methods. Calling methods A static method belongs to the class rather than any instance (object). Therefore, it can be called directly using the class name, without creating an object. Public class methods{ public static void main(String[] args) { MethodName(); }} To call an instance method, you first need to create an object of the class where the method is defined. Then, use the object to call the method. Public class methods{ public static void main(String[] args) { methods obj = new methods(); obj.MethodName(); }} Calling methods Parameters/arguments How do you call a method with arguments/parameters? a value that needs to be passed on method so that the method can use that value and perform various operation on it. For example: public static void main(String[] args) { MethodName(arguments); } accessModifier returnType MethodName(dataTypes parameters){ System.out.println(parameters); } } Parameters/arguments Parameters/arguments Parameters/arguments Parameters/arguments methods with RETURN The return keyword is used to return a value from a method. It is required when a method produces a result. The type of the returned value must match the method's specified return type. Use a specific data type if the method returns a value (e.g., int, double, String). Use void if the method does not return anything. example public static void main(String[] args) { datatype variableName = MethodName(parameter); System.out.print(variableName); } accessModifier returnType MethodName(parameters){ return value; } } Overloading methods Method overloading in Java occurs when two or more methods in the same class have the same name but different parameters. Overloading allows you to define multiple behaviors for a method while maintaining readability. For example: public static int add(int num1, int num2){ return num1 + num2; } public static double add(double num1, double num2){ return num1 + num2; } Thank you