Chapter 7 Quick Reference
To | Do this |
Declare a class | Write the keyword class, followed by the name of the class, followed by an opening and closing brace. The methods and fields of the class are declared between the opening and closing brace. For example:
class Point { ... } |
Declare a constructor | Write a method whose name is the same as the name of the class and that has no return type (not even void). For example:
class Point { public Point(int x, int y) { ... } } |
Call a constructor | Use the new keyword, and specify the constructor with an appropriate set of parameters. For example:
Point origin = new Point(0, 0); |
Declare a static method | Write the keyword static before the declaration of the method. For example:
class Point { public static int ObjectCount() { ... } } |
Call a static method | Write the name of the class, followed by a period, followed by the name of the method. For example:
int pointsCreatedSoFar = Point.ObjectCount(); |
Declare a static field | Write the keyword static before the declaration of the field. For example:
class Point { ... private static int objectCount; } |
Declare a const field | Write the keyword const before the declaration of the field, and omit the static keyword For example:
class Math { ... public const double PI = ...; } |
Access a static field | Write the name of the class, followed by a period, followed by the name of the static field. For example:
double area = Math.PI * radius * radius; |