Chapter 4 Part1
Chapter 4 Part1
Independent function
Example:
public class ExampleStatic
{
private int x; //non-static data
private static int y; //static data
:
public static int showStatic() //static method
{ return y=5; }
}
How would you call the static method?
ExampleStatic.showStatic(); // the method is static and public
y 0
Example: Circle.java (Static Fields and /**Return radius*/
Methods)
public double getRadius() {
public class Circle { return radius;
/** The radius of the circle */ }
private double radius;
/**Set a new radius*/
/**The number of the objects created */
public void setRadius(double
private static int numberOfObjects
= 0; newRadius) { radius =
newRadius;
/**Construct a circle with radius 1 */ }
public Circle() {
radius = 1.0; /**Return numberOfObjects*/
numberOfObjects++;
public static int
}
getNumberOfObjects() {
/**Construct a circle with specified return numberOfObjects;
radius*/ }
public Circle(double newRadius) {
radius = newRadius; /**Return the area of this circle*/
numberOfObjects++;
} public double findArea() {
return radius * radius * Math.PI;
}
} 9
Main method : TestCircle.java
return
11
You may define 2 classes in one file, but only
one class in the file can be a public class
showMessageDialog(null,
showMessageDialog(String) Good Morning!);
Output:
The SimpleDateFormat class allows
the Date information to be displayed
with various format.
Example:
Use a GregorianCalendar object to
manipulate calendar information
Example:
This table shows the class constants for
retrieving different pieces of calendar
information from Date.
Sample calendar retrieval:
Java provides routine for converting String to a
primitive data types or vice versa