In Java, static methods and variables belong to the class and not to any particular instance of the class. This means that they can be accessed without creating an instance of the class, by using the class name instead.
Static variables (also known as class variables) are declared using the static
keyword and are shared among all class instances. They are usually used to store values common to all class objects, such as a constant value or a counter.
Static methods are declared similarly, with the static
keyword. They can be called using the class name, without creating an instance of the class. They are commonly used for utility methods that don't require access to instance-specific data.
It's important to note that static methods and variables cannot access non-static methods or variables, because they are not associated with any particular class instance.
Static Variable
To declare a static variable, you put them in the class just like instance variables, but you put the keyword static in front. Static variables belong to the class and have one copy. When a value is stored in a static variable, it is not stored in the object of the class. Static variables are useful to keep information that is common to every object.
Static Methods
A static method can be accessed by using the name of the class. So you don't need to create an instance to access the method. Methods can also be declared static by placing the static keyword between the access modifier and the method's return type. When a class contains a static method, it is not necessary to create an instance of the class to use the method.
Static methods in a class are useful for several reasons:
Namespace Organization: Static methods help you organize your code by providing a specific namespace for the method. It's clear that the method belongs to the class.
No Instance Needed: Since static methods are bound to the class and not the instance of a class, they can be called without creating an instance of the class. This can be beneficial when you have a method that doesn't use any instance-specific data or methods.
Code Clarity: Using static methods makes it clear that a method doesn't rely on any internal state of the object, and thus reading and understanding the method can be easier.
Reusability and Performance: Static methods can't modify object state or class state, hence they can be reused across instances and are generally more performant because you don't need to instantiate an object to use them. They also can't be overridden by subclasses, providing a degree of protection against changes from future modifications to the code.
Memory Efficiency: Since no instance of the class is needed to execute a static method, memory is saved because the JVM doesn't need to create an object in memory each time the method is invoked.
Utility Functions: Static methods are ideal for utility functions that perform a stateless operation, such as calculations, transformations, or other operations that don't rely on the state of an object.
However, it's important to be mindful of when and how to use static methods. Overuse of static methods (and static state) can lead to code that is difficult to test and maintain. It's often a good idea to limit use of static methods to cases where you are certain that the method does not need to access or mutate any instance or class-level data.
HeaderClass
/** * @author Kevin Roark * Static Header Class * Class methods for use to generate a program header and footer */ public class HeaderClass { //variable to keep track of the times one of the class methods was called private static int numberTimesUsed = 0; //Static Utility to print a header - take a string argument to display public static String PrintHeader(String pText) { String myReturn = ""; myReturn += PrintLine(); myReturn += pText + "\n"; myReturn += PrintLine(); numberTimesUsed++; return myReturn; } //Static Utility to print a footer public static String PrintFooter() { String myReturn = ""; myReturn += PrintLine(); myReturn += "End of Program\n"; myReturn += PrintLine(); numberTimesUsed++; return myReturn; } //Getter to return Utility Method was used public static int getTimesUsed() { return numberTimesUsed; } //Private Methods to use in the class //Method prints a line of Characters private static String PrintLine() { return "######################################\n"; } }//end of class
The provided Java class, HeaderClass
, consists of static methods that generate a program header and footer. These methods can be called directly from the class itself without creating an instance of it. The class uses a private static integer, numberTimesUsed
, to keep track of how many times one of the class methods was called.
numberTimesUsed
: This is a private static variable that keeps track of the number of times the header or footer printing methods have been used.PrintHeader(String pText)
: This is a static method that takes aString
argumentpText
. It creates a stringmyReturn
composed of a line (obtained from thePrintLine()
method), the argument text, and another line. It increments thenumberTimesUsed
variable by one and returns the constructed string. This is used to print a header with the provided text.PrintFooter()
: This is a static method that creates a stringmyReturn
composed of a line (from thePrintLine()
method), the text "End of Program", and another line. It increments thenumberTimesUsed
variable by one and returns the constructed string. This is used to print a program footer.getTimesUsed()
: This is a static method that returns the current value ofnumberTimesUsed
, providing information on how many times the class's header or footer methods have been used.PrintLine()
: This is a private static method that returns a string of the character '#' repeated 38 times, followed by a newline character. This method is used byPrintHeader
andPrintFooter
to generate the lines they include in their output.
This class serves as a utility class for providing headers and footers to other parts of a program. Its methods are all static, so they can be called without creating an instance of HeaderClass
, and it keeps track of usage with a static counter.