// FieldDotDemo.java
// Exemplifies use of dot notation in accessing
// an instance variable. Note that because the
// instance variable is private, it can be
// accessed in this manner only within this class.
public class FieldDotDemo
{
private int i;
private static int sumAll = 0;
public FieldDotDemo(int i)
{
this.i = i; // Here, "this.i" is needed to
// access instance variable i
// as distinct from parameter i.
sumAll += i; // Allowed. But, in a constructor or
// instance method, it would be nicer to
// refer to sumAll as FieldDotDemo.sumAll
// to remind the programmer that it's a
// static variable, not an instance variable.
} // constructor
public int compareTo(FieldDotDemo other)
{
return(i - other.i); // Here, plain "i" is the instance
// variable i for this object,
// whereas "other.i" is i for the
// parameter object
} // method compareTo
public static FieldDotDemo add3(FieldDotDemo x,
FieldDotDemo y,
FieldDotDemo z)
{
return new FieldDotDemo(x.i + y.i + z.i);
} // method addThree
public static int getSumAll()
{
return sumAll; // Here, sumAll is clearly a static
// variable. Instance variables cannot
// be accessed in static methods without
// dot notation.
} // method getSumAll
} // class FieldDotDemo