Upload
emmeline-greene
View
217
Download
0
Embed Size (px)
Citation preview
Static Class Methods
CSIS 3701: Advanced Object Oriented Programming
Need for Static Components
• Calling standard OOP method:
objectname.methodname(parameters)
• Does not make sense for functions:– Example: sqrt function in Math class– Theoretically, must construct a “Math” object first!
Math m = new Math();y = m.sqrt(x);
Assumes that object exists
Static Methods
• Static methods not associated with object
• Syntax: classname.methodname(params)
• Example: All methods in Math class:
y = Math.sqrt(x);
Standard Output
• Printing to “standard output”– Status window in NetBeans– DOS screen when app run independently
• System.out.println(String);
Class representing link to OS standard output object
• Often used for debugging – Displaying breakpoints, variable values– Not used in final applications– Standard input very rarely used
Parsing
• Parsing strings to numeric values
getText() String parsing numeric value for math
• Key: Each simple type has corresponding classint Integerdouble Doubleboolean Booleanchar Char
• Built-in tools to manipulate simple type values
Parsing
• Parsing syntax: type = Classname.parseType(String);
• Examples:int h = Integer.parseInt(hourField.getText());
double area = Double.parseDouble(areaString);
String Format
• Printing numbers with fixed number of digits
String.format(“format", value); same % syntax as C
• Example:
String radiusString = String.format(“%.3f", radius);
3 digits after decimal
Popups
• Static methods in JOptionPane class• Simple message:
JOptionPane.showMessageDialog( location, String);
displayed
• Example:
JOptionPane.showMessageDialog(null, "The radius is " + radiusString);
Usual values: null center of screen this same location as app
Popups
• Prompting for values:String reply = JOptionPane. showInputDialog(location,String);
Returns string entered by user
• Example:
String areaString = JOptionPane.showInputDialog(null, "Enter the area of a circle");
Note: returns null if Cancel button pressed instead
Creating Static Methods
• Syntax:public static returntype methodname (params) {…}
• Are there useful functions that do not require an object?– Example: translating hour, minute to am/pm form in Clock class
public static String ampm(int h, int m) { String post = "am";
if (h > 12) {
h -= 12;
post = "pm";
}
return ""+h+":"+m+post;
}
Call as Clock.ampm(12, 34);
Static Member Variables
• Can declare member variables static:
private static type variablename;
• One variable “shared” by all instances of class
public class Clock {
private int hour;
private static int MAXHOUR;
Clock object hour: 14
Clock object hour: 2
Clock object hour: 12
23MAXHOUR
Static Constants
• Generally used to implement class constants– If value never changes, more efficient to keep single copy
instead of one for each object
• Example: maximum values for hour/minute– Easier to change in future
public static final int MAXHOUR = 23;
public static final int MAXMINUTE = 59;
… public boolean isHour(int h) {
return 0 <= h && h <= MAXHOUR;
}
Java syntax for “constant”
Static Constants
• Often make public to help user– Safe since user cannot change value
public static final int MAXHOUR = 23;public static final int MAXMINUTE = 59;
– Access using syntax classaname.constantname
int h = Integer.parseInt(hourField.getText());
int m = Integer.parseInt(minuteField.getText());
if (h <= Clock.MAXHOUR && m <= Clock.MAXMINUTE) {
clock.setHour(h);
clock.setMinute(m);
}
Static Constants
• Built into many classes– Example: Math.PI
• Often used as flags in place of integers– Example: Alignment of labels
JLabel l = new JLabel(label, alignment);
1 left align2 center align3 right align
Don’t want to have to memorize this!
Static Constants
• Internally (sort of):
public class JLabel …{ public static final LEFT = 1; public static final CENTER = 2; public static final RIGHT = 3;
• Example of use:JLabel timeLabel = new JLabel(“Time: ”, JLabel.RIGHT);
Just have to remember constant names
Static and main
public static void main(String[] args) {...}
• When application started:– main method called– main method calls constructor to create app
• No object exists when main called!
– main method must be static so can be called before any object exists yet