- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Final static variables in Java
Final Static Variables
Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
There would only be one copy of each class variable per class, regardless of how many objects are created from it.
Static variables are normally declared as constants using the final keyword. Constants are variables that are declared as public/private, final, and static. Constant variables never change from their initial value.
Static variables are stored in the static memory, mostly declared as final and used as either public or private constants.
Static variables are created when the program starts and destroyed when the program stops.
Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class.
Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally, values can be assigned in special static initializer blocks.
Static variables can be accessed by calling with the class name ClassName.VariableName.
When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables.
Example
public class Tester { // DEPARTMENT is a static constant public static final String DEPARTMENT = "Development "; public static void main(String args[]) { String salary = "1000"; System.out.println(DEPARTMENT + "average salary:" + salary); } }
This will produce the following result −
Output
Development average salary:1000
Note − If the variables are accessed from an outside class, the constant should be accessed as Employee.DEPARTMENT
- Related Articles
- Static and non static blank final variables in Java
- Assigning values to static final variables in java\n
- What is blank final variable? What are Static blank final variables in Java?
- Interface variables are static and final by default in Java, Why?
- final variables in Java
- Static variables in Java
- Initializer for final static field in Java
- Difference Between Static and Final in Java
- Class and Static Variables in Java
- Why variables are declared final in Java
- What is static blank final variable in Java?
- Can we serialize static variables in Java?
- Difference between constants and final variables in Java?
- Can we declare final variables without initialization in java?
- Are static local variables allowed in Java?\n
