Spaces:
Running
Running
TurkuBasicOOPinJava / Week 6: Methods of OO Programming /01A. Static members [= CLASS MEMBER, not object member]
| 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()); | |
| } | |
| } | |