Static Methods in Java Programming

slide1 n.w
1 / 14
Embed
Share

Explore the concept of static methods in Java programming, which do not require an implicit parameter and are not called on objects. Learn how to create and use static methods, along with examples and illustrations. Discover the versatility of static methods for efficient coding practices.

  • Java Programming
  • Static Methods
  • Object-oriented Programming
  • Coding Practices
  • Java Examples

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. 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.

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

  3. 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.

  4. 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); } }

  5. Example of using static methods Assuming that the MyMethodContainerclass 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.

  6. 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); }

  7. 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.

  8. 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.

  9. 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. 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.

  10. 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.

  11. 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; } }

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

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

More Related Content