Class behaviour are represented in Java by methods. To declare a method use the following syntax:
static methods are shared by all members and exist for all runtime. Static methods can be referenced without creating an instance of the class. abstract methods must be redefined on inheritance. native methods are written in C but accessible from Java.
The return_data_type defines the type of value that the calling routine receives from the object (the reply message in object terminology). It can be any of the primitive types or the reserved word void (default value) if no message is to be returned. The statement return varName; is used to declare the value to be returned to the calling routine.
The parameter_list can contain from zero to many entries of datatype varName pairs. Entries are separated by commas. Parameters are passed by value, thus upholding the encapsulation principle by not allowing unexpected changes or side effects. Object references (such as arrays) can also be passed.
Some examples of method header parameter lists are:
Note: Differences from cpp
Types of methods
Pass by value or reference
[ "public" | "private" | "protected" ] [ "final" ] [ "static" | "abstract" | "native" ] return_data_type method_name "(" parameter_list ")" "{" // some defining actions "}"Accessibility keywords are the same as for properties. The default (ie. omitted) is package (aka friendly) or visible within the current package only.
static methods are shared by all members and exist for all runtime. Static methods can be referenced without creating an instance of the class. abstract methods must be redefined on inheritance. native methods are written in C but accessible from Java.
The return_data_type defines the type of value that the calling routine receives from the object (the reply message in object terminology). It can be any of the primitive types or the reserved word void (default value) if no message is to be returned. The statement return varName; is used to declare the value to be returned to the calling routine.
The parameter_list can contain from zero to many entries of datatype varName pairs. Entries are separated by commas. Parameters are passed by value, thus upholding the encapsulation principle by not allowing unexpected changes or side effects. Object references (such as arrays) can also be passed.
Some examples of method header parameter lists are:
public static void example1() {} public static int add2(int x) {x+=2; return x;} public static double example3(int x, double d) {return x*d;} public static void example4(int x, int y, boolean flag) {} public static void example5(int arr[]) {} // note: this is object
Note: Differences from cpp
Types of methods
Pass by value or reference
No comments:
Post a Comment