Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static Class Methods CSIS 3701: Advanced Object Oriented Programming.

Similar presentations


Presentation on theme: "Static Class Methods CSIS 3701: Advanced Object Oriented Programming."— Presentation transcript:

1 Static Class Methods CSIS 3701: Advanced Object Oriented Programming

2 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

3 Static Methods Static methods not associated with object Syntax: classname.methodname(params) Example: All methods in Math class: y = Math.sqrt(x);

4 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

5 Parsing Parsing strings to numeric values getText() String parsing numeric value for math Key: Each simple type has corresponding class int  Integer double  Double boolean  Boolean char  Char Built-in tools to manipulate simple type values

6 Parsing Parsing syntax: type = Classname.parseType(String); Examples: int h = Integer.parseInt(hourField.getText()); double area = Double.parseDouble(areaString);

7 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

8 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

9 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

10 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);

11 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 23 MAXHOUR

12 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”

13 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); }

14 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 align 2  center align 3  right align Don’t want to have to memorize this!

15 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

16 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


Download ppt "Static Class Methods CSIS 3701: Advanced Object Oriented Programming."

Similar presentations


Ads by Google