Java Tips

(all of this is before my 2008 class - see notes from 2008 class)

(also see OOP Basics or better yet 2008 OOP Basics)

What is a static field?

Static means the variable is shared by all instances of a class. The class does not need to be instantiated prior to accessing one of its static variables. For this reason static variables are also called class variables.

You most often see non-mutable static fields (contants), i.e., their values never change (e.g., Math.PI). The use of non-mutable static fields is common and is not discouraged. You define a non-mutable static field with the key words "static" and "final," as in:

public static final String CERT_PARAMETER = "javax.servlet.request.X509Certificate";

Why should you avoid the use of mutable static fields?

You define a mutable static field by dropping the key word "final." Mutable means the variable is modifiable. Static mutable fields are like global variables in procedural languages but they do not behave the way global variables in procedural languages behave. Java is multi-threaded and every thread accesses the same physical static variables. Multiple threads can represent multiple end users using the same web application. When one of them changes the value of a static variable, it affects all of them. This clearly can have very undesirable and surprising results!

So unless you intend the value of a mutable static field to be shared by all threads, DO NOT USE THEM!


Static methods

Static methods are also shared by all threads but you can protect one user's execution of a static method from being stepped on by another user or thread by using the key work synchronized as in:

public static synchronized Comparator getComparator(String s) ...

The entire snippit of code:

static synchroized method getComparator

 


Overriding methods from the super class Object

There are methods that all Java objects inherit from their super class Object. Some of these methods were designed to be overriden, specifically:

toString()
toString

equals()
equals

hashCode()
hashcode

If toString() is not overriden it returns a string representation of the object that is pretty useless. For example, if toString() is not overriden:

Po po = new Po("4500718933", "VWR SCIENTIFIC PRODUCTS - ECAT ONLY","24.81"));
System.out.println(po);

would return something like the following:
Po@5eb0a9

Overriding the toString() method that is inherited from Object provides a simple, convenient mechanism for debugging classes during development, by translating object state into text. It can also be used at runtime for passing informative error messages to Exception constructors and assertions.

If the above toString() is in the object, Po, the following would be returned from the statement System.out.println(po);

4500718933 VWR SCIENTIFIC PRODUCTS - ECAT ONLY 24.81

If you don't override equals() and hashCode() no two objects will ever be equal because the default behavior of equals() is to determine if 2 objects are references to the same object on the heap and the default behavior of hashCode() is to give each object on the heap a unique hashcode value. If you override one you must override both because if 2 objects are equal they must return the same hashcode.

If you do override these two methods, you can use Collections.replaceAll() to replace an object in an ArrayList, you can use Collections.binarySearch() against an ArrayList sorted using the compareTo() method to find an object within your ArrayList.


Collections

There are two types of general collections that form the basis of all collection classes:

java.util.Map - implements Map<key, value> - Keys must be unique. There can be only one value per key, and it may not be null. For example state abbreviation (key), state name (value):

AL, Alabama
AK, Alaska
AZ, Arizona
AR, Arkansas
...

java.util.Collection - a class which can hold zero, one. or many instances of some other class(es). (In Java 1.4.2 these are weakly-typed as opposed to Java 5 where you can specify the type of object in your collection. All of these examples are 1.4.2 code.)

You can add & remove elements
Clear to an empty set
Report their size

ArrayList is one of the most commonly used collection classes. In Java 5 you can type your ArrayList (e.g., ArrayList<Po> purchaseOrders = new ArrayList<Po>();) In Java 1.4 you can not type your ArrayList but you can still populate them with other classes (e.g, ArrayList purchaseOrders = new ArrayList(); purchaseOrders.add(new Po());) complete snippit of code:

ArrayList of P.O.s

If your Po class overrides the two methods, equals() and hashCode(), which it inherited from Object, then you can replace Po items in your ArrayList purchaseOrders with the Collections method replaceAll():
Collections.replaceAll(purchaseOrders, new Po("4500718933", "VWR SCIENTIFIC PRODUCTS - ECAT ONLY","24.81"), new Po("newPoNumber","newVendorName","newAmount"));

Sorting ArrayList

If your Po class implements the interface Comparable, overriding the compareTo method, you can sort your ArrayList of purchase orders. The Return Value from compartTo is a 32-bit signed integer that indicates the relative order of the objects being compared. If it is less than zero, this instance is less than obj being passed. If it is zero, this instance is equal to obj being passed. If it is greater than zero, this instance is greater than obj.

compareTo()
compareTo

With compareTo implemented in your class, Po, the following Collections methods will work.

Collections.sort(purchaseOrders);
will sort the ArrayList of purchase orders in PO Number order.

Collections.reverse(purchaseOrders);
will sort the ArrayList of purchase orders in reverse PO Number order.

If you want to sort by different column(s) you will need static member classes within your Po class. In order to be comparators, these static member classes must implement the Comparator interface.

private static class CompareDate implements Comparator...

comparator class

In order to use this class as a comparator, it must be instantiated in a static method that returns it as a Comparator.

public static Comparator getComparator() {

return new Po.CompareDate(); ...

entire snippet of code:

method that returns comparator

Now it can be used in Collections.sort. Assuming that purchaseOrders is an ArrayList of Po, the following will sort your ArrayList in date order:

Collections.sort(purchaseOrders, Po.getComparator("date"));

All the code for the Object, Po is in Po.java.

All the code that builds and sorts the ArrayList purchaseOrders is in ListPo.java.

You can use po.txt as your test data.