C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
TotalLength: We see how to directly call a method. We call the totalLength method from main.
AverageLength: With averageLength(), we call another method. This shows methods can call methods.
Info: AverageLength depends on totalLength. Suppose totalLength() is changed. AverageLength() will automatically implement this change.
Java program that introduces methods
public class Program {
static int totalLength(String a, String b) {
// Add up lengths of two strings.
return a.length() + b.length();
}
static int averageLength(String a, String b) {
// Divide total length by 2.
return totalLength(a, b) / 2;
}
public static void main(String[] args) {
// Call methods.
int total = totalLength("Golden", "Bowl");
int average = averageLength("Golden", "Bowl");
System.out.println(total);
System.out.println(average);
}
}
Output
10
5
So: We do not need to remember a different name for "action" in this program. Each call is inferred by its arguments—an int, a String.
StringsJava program that overloads methods
public class Program {
public static void action(int value) {
System.out.println("Int = " + Integer.toString(value));
}
public static void action(String value) {
System.out.println("Length = " + value.length());
}
public static void main(String[] args) {
// Call with Integer argument.
action(1);
// Call with String argument.
action("cat");
}
}
Output
Int = 1
Length = 3
Tip: Methods are by default instance and private. We make the method here public, but leave it as an instance method.
Java program that calls instance method
class Item {
public int size() { // An instance method.
return 10;
}
}
public class Program {
public static void main(String[] args) {
// The size method can only be accessed from an instance.
Item item = new Item();
int value = item.size();
System.out.println(value);
}
}
Output
10
Public: Many classes require entry points other than the constructor. A public method, like apply() here, is an entry point.
Private: Classes contain internal, class-only implementation logic. Private methods work well for this purpose.
Default: The default is package-based. So when you don't specify public or private, the method can be used throughout the entire package.
Java program that uses public, private methods
class Test {
public void apply() {
System.out.println("Apply called");
this.validate();
}
private void validate() {
System.out.println("Validate called");
}
}
public class Program {
public static void main(String[] args) {
// Create new Test and call public method.
Test t = new Test();
t.apply();
// Cannot call a private method:
// t.validate();
}
}
Output
Apply called
Validate called
Note: The default accessibility also allows this. But protected makes this concept explicit.
Java program that uses protected method
class Widget {
protected void display() {
System.out.println(true);
}
}
class SubWidget extends Widget {
public void use() {
// A subclass can use a protected method in the base class.
// ... It cannot use a private one.
super.display();
}
}
public class Program {
public static void main(String[] args) {
SubWidget sub = new SubWidget();
sub.use();
}
}
Output
true
Java program that uses return statement
public class Program {
static String getName() {
// This method must return a String.
return "Augusta";
}
public static void main(String[] args) {
String name = getName();
System.out.println(name);
}
}
Output
Augusta
Java program that has void method
public class Program {
static void test(int value) {
if (value == 0) {
// A void method has no return value.
// ... We use an empty return statement.
return;
}
System.out.println(value);
// A return statement is automatically added here.
}
public static void main(String[] args) {
// No variables can be assigned to a void method call.
test(0); // No effect.
test(1);
}
}
Output
1
Here: We instantiate a Cat object. We then call its isFuzzy boolean method, which always returns true.
Important: This design can be useful in real programs. For example, some Cats might be hairless—isFuzzy would return false.
Java program that uses boolean method
class Cat {
public boolean isFuzzy() {
return true;
}
}
public class Program {
public static void main(String[] args) {
Cat c = new Cat();
// Call boolean instance method.
if (c.isFuzzy()) {
System.out.println(true);
}
}
}
Output
true
Varargs: In older languages like C, this syntax was called varargs. In C# we use the params keyword.
Ellipsis: The ellipsis (three dots) indicates a variable-length array of the type. So the displayAll method receives an int array.
Quote: In computer programming, a variadic function is a function of indefinite arity... one which accepts a variable number of arguments.
Variadic function: WikipediaJava program that uses variable argument list syntax
public class Program {
static void displayAll(int... test) {
// The argument is an int array.
for (int value : test) {
System.out.println(value);
}
System.out.println("DONE");
}
public static void main(String[] args) {
// Pass variable argument lists to displayAll method.
displayAll(10, 20, 30);
displayAll(0);
displayAll();
}
}
Output
10
20
30
DONE
0
DONE
DONE