Tuesday, July 5, 2016

JAVA Methods

In JAVA you can create your own methods and Classes to perform actions that are not currently built into the JAVA language (ie Math.max , .substring, Character.toUpperCase)


method: a group of statements that we give a unique name



Methods can reduce redundancy in your code and setup a clean structure for your code that is easier to modify or debug

There are two type of methods -


static methods: not associated with objects, they are helper functions (ie Math Class and Character Class methods)


member methods: are associate with some object type and work on the data in the object (ie String Class methods)


Creating Static Methods


The syntax for creating static methods looks like -


public static return_type name(parameters)  { 
statement;
statement;
statement;
...
statement;
}

public indicates that anyone can use this method

If there is no return type enter void  

If there are no parameters then the parentheses is left empty ()

When a method is called the program execution jumps to that method, performs the associated statements, then jumps back to the call location to continue the code execution

Parameters

Parameters are passed to a method by the caller (ie numbers given to the Math.max method, or character given to the  Character.toUpperCase method)

When you create your method, you need to declare the type and variable name of the parameters that will be used in the method - When the method is called, the parameters get initialized (values assigned)

Declaring parameters is similar to declaring variables (type and name needed)


public static return_type name(type name, type name, ... , type name)  { 
statement;
statement;
statement;
...
statement;
}

Here is an example:


public static void helloWorld(String sayName)  { 
out.println("Hello World my name is " + sayName);
}

When the method helloWorld is called the parameter for the person's name is initialized using a string type parameter

helloWorld("Martha");


Overloading

You can use the same method name for multiple methods as long as there is a difference in the parameters, for example if you want to create a method to calculateAverage, you can create multiple methods for different types (calculating average of 3 integers, 4  integers, 2 doubles, etc.)


Variable Scope

Variables only exist inside the {   }  in which they are declared

Example:


public static void scopeExample() 

double x=0.1;

{
int  y = 25;
out.println(x + " " + y);
}

// variable y no longer exists

}

//variable x no longer exists


Parameter Scope

A parameter exists only inside the method it is declared in

When the method is called the value of the parameter gets assigned to the parameter name - known as call-by-value or pass-by-value

The called parameter value is maintained even if additional functions are applied to it


Method Return Values

The return type is specified in the method declaration as in the example below:


public static int doubleInteger(int paramInteger)  { 

int result = 2 * paramInteger;

return result;

}

The return statement sends a specific value back to the caller and immediately ends the method and returns to the call location





Class Note about the .out object



The .out object used in out.println is not a globally available object - so it will not work inside static methods that want to use it

To fix this simply remove "static" in declaring methods to get access

System.out is a global object and can be used to print to the desktop computer

out is an object that was created to write output to the android app screen

No comments:

Post a Comment