Site hosted by Angelfire.com: Build your free website today!
       
The Digital Reference Section


JAVA Methods() Structure
by Rodelio P. Barcenas

The structure of methods() can be written in a simple format as shown below.

Action Method w/o parameters
USAGE: objectName.methodName();

public static void methodName(){

... process is written here

}

Action Method w/ parameter
USAGE: objectName.arrest("Bin Laden");

public static void arrest(String name){

... process is written here

}

Accessor Method w/o parameters
Accessor Methods returns a value to the calling program
USAGE: String childName = objectName.getName();

public static String getName(){

String name = "Nathaniel";
return name;

}

Accessor Method w/ parameters
USAGE: boolean valid = objectName.isInteger("12");
if (objectName.isInteger(y)) {

}

public static boolean isInteger(String x){

... program to detect if integer
... return true if integer is found
... else return false

}

Constructor Method
USAGE: this method is only invoked during the time the object is created using the keyword new. The objective of the constructor is to initialize the instance variables of the object. A constructor name should be of the same name as of the object name.

Example.
Below is an object child with a constructor child()

public class child{

String name,gender;

child(String n, String g){

name = n;
gender=g;

}

}

Below is a program which tries to invoke a constructor when the object child is created.

public class createChild{

public static void main(String[] arg){

child a, b;

a = new child("Roniel Dominic","male");
b = new child("Lanie","female");

}

}

The object createChild created two child a and b with different name and gender.

A Top-Down Structure of Methods found in objects

public class objectName{

// instance variables
private dataType a,b,c;

//constructor
objectname(dataType x, dataType y, dataType z){

a=x;b=y;c=z;

}// end of constructor


//start of accesorMethod area

public static String getName(){

String name = "Nathaniel";
return name;

}// end of accessorMethod area

 

// start of actionMethod area

public static void arrest(String name){

... process is written here

} // end of actionMeethod area

} // end of objectName

 


The PinoyTechZone.Com
Copyright 2002-2003
All rights Reserved