Static Variables Methods And Polymorphism Using Overloading
Static Variables
Recall that instance variables are defined for each instance of a class. This has two effects:
Any instance can have unique variable values
An instance must be created in order to make use of instance variables
In contrast, static variables are the same for every instance of a class. You don't even need to instantiate a class to use them.
Pro tip: Static variables are most useful for hard-coded values that you are confident will not change.
Pro pro tip: If you are extremely confident that a static variable will never change, you can use the static final
keyword instead.
Common use cases
One interesting use case is to store every instance created from a class in a static variable:
This allows you to share data across every instance of a class.
Static Methods
Static methods are essentially the same thing as static variables but applied to methods instead.
Note: The public static void main
method, the entry point for your entire application, is actually a static method! The runtime knows to run this static method to begin your application.
Access in static methods
Static methods:
Can only access static variables, not instance variables
Can call other static methods
Have no access to
this
In contrast, instance methods:
Can access static variables
Can call static methods
Common uses cases
Helper methods
Example:
Math.sqrt
Methods that don't require access to an instance and its variables to compute data
Polymorphism and Overloading
Method signatures
In Java, you can make multiple methods with the same name as long as they have either different types or sequences for their parameters:
These are known as the signature of a method.
Overloading
Polymorphism means "many shapes".
This breaks up into 2 distinct forms of polymorphism:
Overloading
Creating multiple methods with the same name but different signatures
Overriding
Overriding a class's inherited methods with another method with the same signature
Why overload a method
Overloading allows you to accommodate multiple data types. For example, System.out.println
supports Strings, ints, doubles, etc. because it's been overloaded.
Also, overloading allows you to create a default case:
Finally, overloading allows you to stack possible outcomes for your methods:
Constructor overloading
Constructor overloading is method overloading but with 2 extra rules:
Use the
this
keyword to call a constructor inside another constructorThe use of
this
to call a constructor must be the first thing the new constructor does
Best practice: Use constructor overloading when you want to do essentially the same thing but with different parameters. For example:
Last updated