16
Static Class Methods CSIS 3701: Advanced Object Oriented Programming

Static Class Methods CSIS 3701: Advanced Object Oriented Programming

Embed Size (px)

Citation preview

Page 1: Static Class Methods CSIS 3701: Advanced Object Oriented Programming

Static Class Methods

CSIS 3701: Advanced Object Oriented Programming

Page 2: 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

Page 3: Static Class Methods CSIS 3701: Advanced Object Oriented Programming

Static Methods

• Static methods not associated with object

• Syntax: classname.methodname(params)

• Example: All methods in Math class:

y = Math.sqrt(x);

Page 4: Static Class Methods CSIS 3701: Advanced Object Oriented Programming

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

Page 5: Static Class Methods CSIS 3701: Advanced Object Oriented Programming

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

Page 6: Static Class Methods CSIS 3701: Advanced Object Oriented Programming

Parsing

• Parsing syntax: type = Classname.parseType(String);

• Examples:int h = Integer.parseInt(hourField.getText());

double area = Double.parseDouble(areaString);

Page 7: Static Class Methods CSIS 3701: Advanced Object Oriented Programming

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

Page 8: Static Class Methods CSIS 3701: Advanced Object Oriented Programming

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

Page 9: Static Class Methods CSIS 3701: Advanced Object Oriented Programming

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

Page 10: Static Class Methods CSIS 3701: Advanced Object Oriented Programming

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

Page 11: Static Class Methods CSIS 3701: Advanced Object Oriented Programming

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

Page 12: Static Class Methods CSIS 3701: Advanced Object Oriented Programming

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”

Page 13: Static Class Methods CSIS 3701: Advanced Object Oriented Programming

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

}

Page 14: Static Class Methods CSIS 3701: Advanced Object Oriented Programming

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!

Page 15: Static Class Methods CSIS 3701: Advanced Object Oriented Programming

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

Page 16: Static Class Methods CSIS 3701: Advanced Object Oriented Programming

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