Creation, which defines a starting point or runnable node, should occur within a class. Each method should perform one task or several closely related tasks. Access by other objects is determined by the modifier given to the method.
Returning a value from a method requires two things: the data type of the returned value in the method definition and a return statement in the body of the method. An example of a method with a return statement is as follows:
1 class Person {
2
3 public String fullName;
4 private int age;
5 public char gender;
6 public static int retirement Age = 65;
7 // This is a no argument constructor
8
9 public Person(){
10 fullName = "John Doe";
11 age = 0;
12 gender = '?';
13 }
14
15 // This constructor receives an String to initialize the name
16
17 public Person(String aNewName){
18 this(); // We are calling the first constructor
/* From Developer.com The keyword "this" has three basic uses:
1. To bypass local variables or parameters that hide member variables having the same name, in order to access
the member variable.
2. To make it possible for one overloaded constructor to invoke another overloaded constructor in the same class.
3. To pass a reference to the current object to a method belonging to a different object (as in implementing
callbacks, for example).
*/
19 fullName = aNewName;
20 System.out.println(this);
21 }
22
23 public void printName(){
24 System.out.println(fullName);
25 }
26
27 public void setFullName(String newName){
28 fullName = newName;
29 }
30
31 public String getFullName(){
32 return fullName;
33 }
34
35 public int getAge(){
36 return age;
37 }
38
public void setAge(int aNewAge){
40 age = aNewAge;
41 }
42
43 public static int getRetirementAge(){
44 return retirementAge - age ;
45 }
46
46 }