30 October 2020

Constant variable, static fields and static methods. How they work and how to use them.

Welcome to another tutorial on the "How to code in Java" tutorial series. In the previous tutorial, we've gone through on how to access modifier works while going through the different type of it and how to use each of them correctly. In this tutorial, we'll be looking into what are constant variable, static fields and static methods. 


Index for the tutorial:



Static class members. (fields and methods)


A static class member is used to call or use a class's methods or fields without first creating an object or instance for the class. To create a static method or field, we need to use the "static" keyword in front of the variable or method initialisation.


Creating a static field and method.


To call the class's method and field, you need to use the class name. Then, using the dot notation following with the field/method name. 


Using static field and methods in the main method.


When using static fields and methods, we also need to take note of the restriction of a static field and methods. 

  • A static method cannot call or use an instance method or field which are not static. (require the creation of an object)
  • A constructor cannot be static.
  • A static method could be overloaded but cannot be overridden 


Restriction on static class members.


A non-static method can call methods and field that's static. They can also be used to manipulate the value of a static field. Here's a full example of the static members' usage.


Testing full code for demonstrating static method and field.


The class used for the operation.

result for the code.


You can decide to use a static method or field when you found out that you need the same piece for a specific operation over and over again. Another thing is that you can consider using static methods and field when you only need one copy of the class.



Constant variables.


A constant variable is a type of variable whose content will never be changed again after the initial assignment. To create a constant variable in Java, all you need to do is to put the "static final" keyword in front of the variable declaration. 


The reason why we make the variable as a static field is that, since no changes will be made to its value, every object would have the same value for the variable. That's why using a static variable would decrease the memory needed as we only need to create the value once. While the "final" keyword is used to make the variable value immutable, or unchangeable.


Creating a constant variable.


Now, to demonstrate that a constant variable's value cannot be changed, here's a list of operation that I have tried to change the constant variable's value. None of them will work as explained. 


Demonstrating that a constant variable's value cannot be changed.


The concept goes the same to when referencing a variable to another object. When you initialise a new object, and it's referenced to a constant variable (using the "final" keyword), after the initialisation you can no longer change which object it is referencing to later on. The variable will now only point to one object's location.


A constant variable referenced to an object cannot change its reference to another object.


Although the variable that's holding the reference to the object is a constant, it doesn't mean that we cannot change the field's value that's inside the object. You'll need to make sure that the field's value could be changed by either making the field with a public access modifier or use a setter and getter to manipulate and access the field's value. 


The object's field value can be changed although the variable referencing to the object is constant.


Getter and setter will be discussed in the future tutorial where we'll going into abstraction and encapsulation. For now, know that a setter is used to change the value of a field with private access modifier. While a getter is used to get the value of an object's field that has a private access modifier applied to them.


You can consider using a constant variable for storing values that are meant to be the same during the runtime of a program—for example, the paying rate for the employee of a company.



Source Code



Well, that's pretty much all the things I can explain about constant variables, static methods and static fields. I hope you now know how to use constant variables and static methods and field. If you find this tutorial helpful, please share it with others that might need it or those that might be interested in it. As always, if you have any questions, critics and improvement that could be made, feel free to comment in the comment section below. In the next tutorial, we'll be going into what are pass-by-value and pass-by-reference in java. 

No comments:

Post a Comment