Marco Colombo

Experimental area · proceed at your own risk!

Some hints for Java

Fields and methods

Any object is composed by two parts: fields and methods.

The fields of an object are the variables that belong to the object.

public class Matrix {
  private int    rows;
  private int    cols;
  private double array[][];
}

Any time an object is created, space for those variables is allocated.

The methods of an object are the functions declared inside the class.

public class Matrix {
  public Matrix();
  public Matrix plus(Matrix B);
}

Public and private

The fields of an object, as well as the methods, can be either public or private.

public  int rows;
private int cols;

A public variable can be accessed from outside the class. On the other hand, a private variable cannot be referred to directly from outside the class.

Matrix A = new Matrix();
A.rows = 7;
A.cols = 0;  // the compiler will not accept this

The general rule is the following: define private fields and public methods.

Fields should be private because we want the information stored inside an object to be always consistent. Therefore we disallow the user to manipulate directly the fields of an object.

Methods should be public because we want the user to use them.

Debugging

Always start from the first error, as the following ones may just be spurious and disappear after having fixed the first one.

Read the error message, as it provides very important information.

ReadString.java:8: package Sstem does not exist
      Sstem.out.print("Enter your name: ");
           ^

The error message points at the place where the compiler has noticed an error, which is line 8 of ReadString.java: no package called Sstem exists.

The line number and the pointer refer to the place where the compiler has noticed the error.

While in some cases this corresponds to the real location of the error, in others it does not. In these cases, the error can be located only before.

Static methods

Methods are generally defined like this:

public Matrix plus(Matrix B);

In such a case, the method is said to be non-static, and it has to be used with an existing object, as such:

Matrix A;
A.plus(B);

Instead, a static method is one that can be called independently of an existing object.

public static Matrix random();

Therefore, we can call the method directly by using its complete name:

Matrix R = Matrix.random();

It makes sense to declare a method to be static when the operations defined for it are supposed to be independent of any existing object.

Dates

To represent dates, Java offers the Calendar class and the Date class. Depending on which one will be used, at the top of the file we will need one of the following statement.

import java.util.Calendar;
import java.util.Date;

The Calendar class makes it very easy to find the current date, or to set up a specified date.

// current date
Calendar now = Calendar.getInstance();

// date: 11/10/04 09:00:00
Calendar two = Calendar.getInstance();
two.set(2004, 10, 11, 9, 0, 0);

The Date class stores the date as the number of milliseconds from the 1st January 1970. It is possible to transform the date from the Calendar class into the Date class.

Date nowDate = now.getTime();

Highlights

Links

2004, 2005, 2006, 2007 © marco