Quick Index
General Method Types


  • Constructor
  • Instance Method
  • Instance Method
  • Instance Method
  • Static Method
  • Static Method

  • The keyword static makes a method static
  • We are primarily interested in instance methods and constructors
public class BrilliantNumber {

	private int number;

	
1
public BrilliantNumber(int newNumber)
{ this.number = newNumber; }
2
public int square()
{ int num; num = this.number; return num * num; }
3
public int cube()
{ int num; num = this.number; return num * num * num; }
4
public void setNumber(int aNumber)
{ this.number = aNumber; }
5
public static void main(String[] args)
{ BrilliantNumber bn = new BrilliantNumber(10); System.out.println("Square: " + bn.square()); }
6
public static int getDefaultNumber()
{ return 10; } }

Helper Methods


Helper methods are simply another name for instance methods that help to simplify your code.

For example, let's say you used this same (redundant) code in four different methods in a class:

this.duration = 0;
this.correct = 0;
this.incorrect = 0;


You might add a helper method something like this:

public void reset() {
	this.duration = 0;
	this.correct = 0;
	this.incorrect = 0;
}


Then you could simplify the redundant code to:

this.reset();


We'll dig deeper into this in an upcoming chapter.