Spaces:
Running
Running
File size: 5,130 Bytes
3d70db8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | We have already seen many examples of 'static' methods.
In fact, before we started discussing our own classes, all the methods we wrote were 'static'.
But what are the static methods?
A 'static member' is a 'class member'.
On the other hand, 'object members' are always connected to an object, meaning that 'we need to create an object to call them'.
For example
Student s = new Student("Simon Student", "02-123456", "99999");
System.out.println(s.getName());
Hence, 'object member' is connected to one object created out of the class.
Method getName returns the name of a single student.
========================================
Static methods
Static method is a 'method of a class'.
The method declaration includes the keyword 'static'.
When a method is static...
it can be CALLED WITHOUT CREATING an OBJECT out of the class,
it is called by using the CLASS NAME, not an object reference, and
it is not directed towards any object.
Hence, static methods are often "TOOL METHODS", which get the needed information as arguments.
They usually return a value, or perform a documented side effect to an object received as an argument.
Let's see an example - a static method called 'emailOK' in class 'Person':
class Person {
private String name;
private String email;
public Person(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public static boolean emailOK(String email) {
if (email.length() < 5) {
return false;
}
if (!email.contains("@")) {
return false;
}
if (!email.contains(".")) {
return false;
}
return true;
}
}
The method returns 'true', if the email seems to be valid.
Method can be called from ANY OUTSIDE CLASS using the class name:
public class Test {
public static void main(String[] args) {
System.out.println(Person.emailOK("ernest@example.com"));
boolean mailOk = Person.emailOK("address@madeup");
System.out.println(mailOk);
}
}
As seen above, calling method does not require creating an object - the class name is enough.
Hence, the client can use the method to validate any email address.
Method can naturally be called from the objects created from the class.
Still, the method is NOW called by using the class name.
Hence, we can add the email validation into constructor:
class Person {
private String name;
private String email;
// CONSTRUCTOR
public Person(String name, String email) {
this.name = name;
// ADD 'use of static method' here
if (Person.emailOK(email)) {
this.email = email;
} else {
this.email = "";
}
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
// STATIC METHOD REMAINS
public static boolean emailOK(String email) {
if (email.length() < 5) {
return false;
}
if (!email.contains("@")) {
return false;
}
if (!email.contains(".")) {
return false;
}
return true;
}
}
Let's look at another example. Class 'Point' has a 'static' method for calculating the distance of a point from the origin:
class Point {
private int x;
private int y;
// constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
// STATIC METHOD = class member
public static double distanceFromOrigin(Point point) {
return Math.sqrt(point.getX() * point.getX() +
point.getY() * point.getY());
}
}
Again, the static method can be called
- from another class, or
- inside object methods in the same class.
Let's utilize the 'static' method and write an object method called getDistance.
Note how the object method class the 'static' method and provides the object itself as an argument (by using the 'this' keyword).
class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
// OBJECT METHOD calling STATIC METHOD
public double getDistance() {
return Point.distanceFromOrigin(this);
}
// STATIC METHOD = STATIC MEMBER = CLASS MEMBER
public static double distanceFromOrigin(Point point) {
return Math.sqrt(point.getX() * point.getX() +
point.getY() * point.getY());
}
}
|