MODULE 4 JAVA CLASSES AND METHODS CHAPTER 1 Java Methods
Program Modularity 4. Java Classes and Methods In functional decomposition, a complex problem is broken up into modules or functional parts. This way, programming work can be distributed, thereby making application development faster. Although modules interact with each other, the tasks contained in a module are independent from other modules. Each module can stand on its own
Program Modularity The same is true in designing programs. A complex program that accomplishes many objectives needs to be broken down into functional modules. These modules are called methods.. Each method performs a specific task and is independent, or can stand on its own. 4. Java Classes and Methods
Program Modularity However, it remains a part of the class and is called or invoked by the main method. Other methods can also call or invoke a method. In Java, methods are part of the class they belong to, and hence, they are class members. 4. Java Classes and Methods
Method Declaration Is a block of code or a set of statement(s) that accomplish a specific task. Example: public void setName (String temp) { name = temp; } In the example, the method setName has only one function or purpose: to set the value of an instance variable name. 4. Java Classes and Methods
Method Declaration Methods define the behavior(s) of a class. They are members of the class. They are usually located after declaration of the fields or variables of the class, and before the main() method 4. Java Classes and Methods
Method Declaration To declare a method: access-modifier return-type < methodName > (parameter list) { [statement-1]; [statement-2]; [return] < variableName >; } 4. Java Classes and Methods
Structure of a Method 4. Java Classes and Methods
Structure of a Method Method is made up of two main parts: - method signature - method body 4. Java Classes and Methods Method Signature Method Body
Structure of a Method Parts of the method - Method Name An identifier which is usually a verb that describes what the method does. - Return Type All methods in java are required to have a return-type A method that returns a value should specify the data-type of the return value. This could be any one of the primitive data types or a reference type If the method does not return any value, the return-type is void. 4. Java Classes and Methods
Structure of a Method Parts of the method - Return Type Example 1: double computeAverage () The method computeAverage () returns whose data type is a double Example 2: void printMessage () The method printMessage () does not return a value. Hence, the return-type is void. 4. Java Classes and Methods
Structure of a Method Parts of the method - Method Body Contains statement(s) that are executed when the method is called. Example: void computeAverage (){ average = (( mathGrade + tleGrade + peGrade )/3); } The method body has 1 statement that adds up 3 grades and divides the sum by 3. 4. Java Classes and Methods
Structure of a Method Parts of the method - return Statement Returns a value to the calling method. The value being returned should have the same data-type specified as that specified in the method declaration . Example : double computeAverage (){ double average = (( mathGrade + tleGrade + peGrade )/3); return average } 4. Java Classes and Methods
Structure of a Method If the method’s return-type is void, there is no need for a return statement. A method can have only one return statement. 4. Java Classes and Methods
Structure of a Method Parts of the method - parameter list or argument list Aside from returning a value, a method can also accept values passed by other methods. This is done by specifying parameters after the method name. Parameters are enclosed in parentheses () and separated by commas. Parameters are variables which will hold values that will be used inside the body of the method. 4. Java Classes and Methods
Structure of a Method Parts of the method - parameter list or argument list Example: 4. Java Classes and Methods double computeAverage (double mGrade , double sGrade , double eGrade ) { double average = ( mGrade + sGrade + eGrade ) / 3 return average; } Variables in a parameter list should be declared, e.g., the data-type must be specified
Structure of a Method Parts of the method - access modifier Specifies which classes can access or call the method. If an access modifier is not specified the default access modifier is applied. 4. Java Classes and Methods
Coding Simulation 3. Java Fundamentals Part 2: Control Structures and Arrays
Variables in Method The body of a method contains statements that use variables. Variables declared inside a method are called local variables. Local variables are visible only to the methods where they are declared. They are not accessible from the rest of the class. 4. Java Classes and Methods
Scope of a Variable The scope of a variable determines where the variable can be accessed and how long it will exist in memory. A variable’s scope is: inside the block where it is declared. All statements that will use the variable have to come after the declaration statement. If the variable is declared in an inner block, it cannot be accessed in the outer block since it no longer exists in the outer block 4. Java Classes and Methods
Scope of a Variable Example public static void main (String args [] ) { int i = 0; { int j = 0; } } The variable i is accessible inside the main method, and in the inner block. The variable j is accessible inside the inner block only 4. Java Classes and Methods
Passing Parameters There are two types parameters passed to a method. Those that are passed by value and those passed by reference Pass-by-Value : All primitive data types that are passed to a method are passed by value. When a variable is passed-by-value to a method, the actual value of the variable is received and stored in the method’s parameter list. Inside the method, changing the value of the parameter will not affect the original variable passed 4. Java Classes and Methods
Passing Parameters Pass-by-Reference : Variables that are passed-by-reference are: Strings, arrays and objects. When a variable is passed-by-reference to a method, a reference or pointer to an actual value is passed. The parameter in the method declaration receives the reference or pointer. Inside the method, changing the value of the parameter can change the original values since the reference points to the location where the actual values are stored 4. Java Classes and Methods
Types of Variables Types of variables : Instance Variables (or non-static fields) Class Variables (static fields) Local Variables Parameters 4. Java Classes and Methods
Types of Variables Instance Variables (non-static fields) - An object’s state is stored in instance variables, or variables without the static keyword. - Each instance of a class, or object, has its own copy of instance variables. Changing the value of a variable does not affect other objects instantiated from the same class . 4. Java Classes and Methods
Types of Variables Class Variables (static fields) - Variables declared with the static keyword. - There is only 1 copy of a static variable, therefore, any changes to its value affects other instances of the class. 4. Java Classes and Methods
Types of Variables Local Variables - Temporary variables declared and used in a method. - Local variables are only visible in the method where they are declared. They are not accessible outside the method 4. Java Classes and Methods
Types of Variables Parameters - Temporary variables declared in the method definition. - Like local variables, they exist only in the method and are not accessible outside the method. 4. Java Classes and Methods
Static vs Non-Static Method Static Method - A static method means that it can be accessed without creating an object of the class. 4. Java Classes and Methods
Static vs Non-Static Method Non-Static Method - A non-static method means that an instance of a class (object) should be created before you can access the non-static method. 4. Java Classes and Methods
Coding Simulation 4. Java Classes and Methods
Sources https://www.tutorialspoint.com/java/index.htm https://www.javatpoint.com/java-tutorial https://dev.java/learn/getting-started/ https://www.w3schools.com/java/default.asp Next Chapter: Introduction to Object-Oriented Programming 3. Java Fundamentals Part 2: Control Structures and Arrays