Overriding Methods

When overriding methods the overriding method must not throw checked exceptions that are new or broader than the those declared by the overridden method.

The overriding method can however throw any unchecked exception (i.e. RuntimeException) regardless of whether the overridden method declares the exception.

The access level can be less restrictive than that of the overridden method.

The access level cannot be more restrictive than that of the overridden method.

The overriding method can throw “narrower” or fewer exceptions. The subclass method may not take the same risks, so it need not declare them all.

Watch Out!

class Animal {
public void eat() throws Exception {
System.out.println("Om Nom … Continue Reading

Declaring Enums

In Java 5.0 Sun introduced a new way to create Enumerations, which meant that the enum modifier came into existance.

A simple enumeration example might look like the following.

enum Drink {
	CARBONATED, STILL, HOT, ICED
}

The enumeration values (CARBONATED, STILL, …) are all public static final instance variables, so they can be accessed using.

Drink drink = Drink.STILL;

Enumerations can also be defined within a Class.

public class Thirsty {
	enum Drink {
		CARBONATED, STILL, HOT, ICED
	};

	Drink drink = Drink.HOT;
}

Strangely the semi-colon (;) at the end of the enum declaration is optional.

You cannot define an enum within a method.

Additional information can be stored in enum classes per enumeration entry, below shows … Continue Reading

static

The static keyword (modifier) is used to signify that the method, class or instance variable exists without the class needing to be instantiated first.

Methods, Variables (instance), Nested classes, and Initialization blocks can be marked as static.

You cannot make a class defined in a method static, i.e. a class local to a method.

volatile

The volatile keyword (modifier) tells the JVM that a thread accessing the variable must always reconcile its own private copy of the variable in memory with the master copy in memory.

We should remember that volatile can only be used on instance variables, similar to the transient keyword.

transient

The transient keyword is used on instance variables to signify that when the owning class is serialized that this property is not to be saved/sent.

final

final can mean a number of things.

  1. public final void energise() { }
  2. public final int length = 7;
  3. public final List people = new ArrayList();
  4. public final class Neutron { }

In example 1. we see a method that has been declared as final because the developer did not want this method to be abused or changed. We might see methods declared in this way in a game for example to prevent cheating.

In example 2. we see a primitive being assigned as final. This means the value 7 which was assigned to it at the declaration will remain the same forever. We cannot assign a … Continue Reading

Array Declaration

Arrays can be declared in the following code fashion.

int[] lotteryNumbers;
int lotteryNumbers [];

The most readable and recommended array definition is the first example, the second example is legal and will compile.

Below we can see multi-dimensional arrays, the second example shows a multi-dimensional with the square brackets split around the variable name. This square bracket split example is also legal and will compile.

int[][] lotteryNumbersByWeek;
int[] lotteryNumbersByWeek [];

Don’t forget that the size of the array does not matter until it is initialized. Therefore the following example is illegal.

int[6] lotteryNumbers; // illegal declaration.

Shadowing

The practice whereby you have two variables of the same name is known as shadowing. The following class will compile, even with variables of the same name being initialized and passed in as a parameter.

public class MyClass {
	private int shadowed = 5;

	public void doSomething( int shadowed ) {
		this.shadowed = shadowed;
	}

	public void doAction() {
		int shadowed = 10;
	}
}

Variables, Methods & Modifiers

Local variables & method arguments (parameters)

  • final

Variables (class/interface level)

  • final
  • public
  • protected
  • private
  • static
  • transient
  • volatile

Methods

  • final
  • public
  • protected
  • private
  • static
  • abstract
  • synchronized
  • strictfp
  • native

Var-Args, Variable Arguments

Variable argument methods were introduced in Java 5.0. They allow methods to take between 0 and infinity (within memory limits!) arguments of a specific type.

public void varArgMethod(int… x) {
for (int i=0; i<x.length; i++) {
System.out.print(x[i] + “, “);
}
}

The variable argument is defined by adding three dots, i.e. int… value, which must be the last variable taken on the method signature.

The method above can be called in any of the ways shown below.

varArgMethod();
varArgMethod(1);
varArgMethod(1,2,3);
varArgMethod(1,2,3,4,5,6);
varArgMethod(1,2,3,4,5,6,7,8,9);

The var-args parameter can be … Continue Reading