Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation.

Similar presentations


Presentation on theme: "Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation."— Presentation transcript:

1

2 Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation provided an example where neither an implicit nor an explicit parameter were required. The methods in the Math class exemplify the more typical case, where some useful computation is done on explicit parameters and the result is returned.

3 Static Methods, cont. These useful methods belong to the class that contains them. They may be written to take simple types or object references of any class as explicit parameters. Calls to static methods take this general form: result = ClassName.method(parameter(s)); A specific example that we’ve seen is: double x, y; … y = Math.sqrt(x);

4 The main() method is a static method The key word “static” is used when declaring such methods. You have seen this used in writing programs: public class MyProg { public static void main(String[] args) { … The main() method in programs is a static method. The program class itself is simply a container for this useful method. The class does not have a constructor. The system calls the static method directly in order to run the program.

5 Creating static methods It is possible to write your own static methods that take explicit parameters and do some calculation or produce some other desired result. Here is a brief example: public class MyMethodContainer { public static double findPercent(double first, double second) { return ((first / second) * 100.0); }

6 Example of using static methods Assuming that the MyMethodContainer class is in the same package as the program, here is a small program that would use the static method: public class TestContainer { public static void main(String[] args) { double x = 15; double y = 20; double percentResult = MyMethodContainer.findPercent(x, y); System.out.println(x + “ is “ + percentresult + “ % of “ + y); } You would expect to get 75.0% as the result.

7 Static method with object parameters The class TestContainer might also contain a method like this, which illustrates taking an object reference as a parameter. public static double findPercent(Cup5 first, double second) { return ((first.getSeedCount() / second) * 100.0); }

8 Static variables In addition to static methods, Java supports static variables. These are variables which are declared in a class but which differ from instance variables in this way: There is only one copy of the variable, and it is maintained in the class; objects, namely instances of the class, do not receive separate copies of this variable.

9 Static variables, cont. The static variable may be declared private. If so, it is not directly accessible from outside the class. However, it is directly accessible to every object of the class. A static variable may also be declared public. You have already encountered such variables. The mathematical constants PI and E in the Math class are public static final doubles.

10 Example of Static Variables It would be possible to declare a minimum seedCount for objects of the cup class. The example below shows the declaration and initialization of this variable. It is declared final. That means that the value of the static variable can’t be changed.

11 Example of Static Variables, cont. Since there is only one copy of the minimumSeedCount variable for the whole class, this variable can be initialized when it is declared. It doesn’t make sense to assign it a value in a constructor that is called every time a new object of the class is created. The example also shows a constructor and the setSeedCount() method rewritten so that the value of seedCount couldn’t go below the value of the static variable.

12 Example of Static Variables, cont. public class Cup6 { private int seedCount; private static final int minimumSeedCount = 0; … public Cup6() { seedCount = minimumSeedCount; } … public boolean setSeedCount(int seedCountIn) { if(seedCountIn >= minimumSeedCount) { seedCount = seedCountIn; return true; } else return false; } … }

13 A static variable without final It would also be possible to declare and initialize the static variable so that it isn’t final: private static int minimumSeedCount = 0; Then a static method could be added to the class that would allow the minimum to be changed: public static void changeMinimum(int newMin) { minimumSeedCount = newMin; } Notice that this method does not change the instance variable for an object. It changes the minimumSeedCount that the class maintains for all objects of the class.

14 Be careful with certain design changes By adding this static instance, it is no longer suitable to construct instances with a seedCount of initially zero. So the default constructor (the constructor with no arguments) that does this would need to be removed The constructor would probably look something like this: public Cup6(int seedCountIn) { if(seedCountIn >= minimumSeedCount) seedCount = seedCountIn; else seedCount = minimumSeedCount; }


Download ppt "Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation."

Similar presentations


Ads by Google