Create Your Own Function
First lets see what a function looks like. The following function accepts two integers and returns an integer. The return value will be the greater of the two integers passed in.
public static int max(int x,int y)
{
if( x>y )
return x;
else
return y;
}
Consider the following sections of the above method. They are summarized in Table 6.1.
Table 6.1: Sections of a Method
| The return type: | int |
|---|---|
| The parameters: | x & y, both int’s |
| Returns: | The two return statements |
| The modifiers: | static and public |
Calling the above method would be very easy. To assign the variable “i” to the maximum of 10 and 100, you would do the following.
int i = max(10,100);
A function without a return type is called a method. Consider the following method.
public static void printName()
{
System.out.println("Java");
}It looks just like a function except the return type is void, which means no return type.
Your Own Static Methods
Using static on a method or function causes it to operate at the class level. Normally a method operates at the instance, or object, level. If you have not worked with object oriented design, understanding the difference between instance and class level may be somewhat confusing. First, make sure you know the difference between a class and an object.
Table 6.2: The Difference Between a Class and an Object
| Class | A class is a type of something, you do not use the class directly. (i.e. Toyota™ Rav4™) A class name usually starts with an uppercase letter. |
|---|---|
| Object | An object is one instance of a class. (i.e. my blue Toyota™ Rav4™) A object name usually starts with a lowercase letter. |
Most of the methods and functions that we have examined this far have always been static. But what exactly does static method or function mean? First lets examine a program that makes use of a static function. The following program has one method that will return the value passed to it multiplied by ten. This class creates a method named multiply. Notice that the multiply method is static, just like the main method. This means that the main method can call the multiply function without having to instantiate the MyClass class. Listing 6.5 shows a static method.
Listing 6.5: Using a Static Function (MyClassStaticFunction.java)
public class MyClassStaticFunction
{
public static int multiply(int i)
{
return(i*5);
}
public static void main(String args[])
{
int j = 5;
j = multiply(j);
System.out.println(j);
}
}Because multiply is declared static you do not need to use new to create a new instance of the class. You can simply access the multiply method directly from the main method. While this does make it easier to call a method, it prevents the method from using any instance variables. Static methods may not access nonstatic instance variables. This is a fairly big limitation, and as a result most methods are declared nonstatic.
Your Own Nonstatic Methods
Most of the functions and methods that you will create as a Java programmer will not be static. Static methods and functions cannot access instance variables, therefore, sometimes it is best to instantiate the class, and then use it by calling a nonstatic method. Listing 6.6 instantiates and uses a nonstatic method.
Listing 6.6: Using a Nonstatic Method (MyClassNonStaticMethod.java)
public class MyClassNonStaticMethod
{
public int total; // instance variable
public void add(int i)
{
total = total + i;
}
public static void main(String args[])
{
MyClassNonStaticMethod myObject =
new MyClassNonStaticMethod();
MyClassNonStaticMethod myOtherObject =
new MyClassNonStaticMethod();
myObject.add(5);
myOtherObject.add(3);
myObject.add(10);
myObject.add(10);
System.out.println("Total myObject is:" +
myObject.total );
System.out.println("Total myOtherObject is:" +
myOtherObject.total );
}
}
As you can see, there are two object instances created: myObject and myOtherObject. This causes two separate instances of the total instance variable to be kept. The add methods, when called will add the number to their object’s instance of x. As a result this program prints out 15 and 13.




