Functions/Methods

Group your code

General

What is a method?

  • A set of code referred by name
  • You can call (invoke it)
  • It might or might not accept input data
  • It might or might not return result data
  • A method has a start and an end
  • Method can act on data outside its declaration
  • A method with return type MUST return this type
  • A method can call others it itself
  • A method call creates a stack entry
  • The compiler is free to optimize (inlining)
public void show()
public int show()
public Foobar show()
public void show(int i)
public void show(int i, String a)
public Foobar show(String a, String b)
public Foobar show(String c, String d)
public Foobar show(String... c)
public Foobar show(String a, String... c)
public Foobar show(String[] c)
public Result show(String[] c)

Method Signature

When is a method a method and unique?

[visibility] [static] <return-type>> name([Type name]*) (throws Exception[,Exception2]*)*
  • Visibility: public, private, protected, default
  • static: Method owner modifier
  • Return Type: void or ONE other type
  • Name: Name according to guidelines
  • Parameters: No or many type-name pairs, name must be unique
  • throws: Declared exceptions that could come up, no must
  • Only method name and parameters types are the signature!
  • Return type is not part of it
  • Visibility and modifier are not part of it

Method Signatures

Examples

public int show()
public void show()
public int show()
private int show()
protected int show()
private static int show()
public int show(String a)
public int show(String b)
public int show(String a, int b)
public int show(String b, int a)
public int show(String c, int d)
public int show(String e, int f)
public int show(String[] s)
public int show(String... s)
public int show(String c, String... s)
public int show(String... s)
public int show()
public void show(List<String> l)
public void show(ArrayList<String> l)
public void show(Vector<String> l)