Static variables in Java
Static vs Instance Variables
Whenever a variable is declared as static, this means there is only one copy of it for the entire class, rather than each instance having its own copy.
Static variable
When a static variable is declared, a single copy of the variable is created and shared among all objects at the class level. Static variables are, essentially, global variables. All instances of the class share the same static variable.
Important points to note on static variables
We can only create static variables at the class level.
static block and static variables are executed in the order they are present in a program.
Static variables can be called directly with the help of the class only, we do not need to create an object for the class in this.
// Java program to demonstrate execution // of static blocks and variables class Test { // static variable static int a = m1(); // static block static { System.out.println("Inside static block"); } // static method static int m1() { System.out.println("from m1"); return 20; } // static method(main !!) public static void main(String[] args) { System.out.println("Value of a : " + a); System.out.println("from main"); } }
The results from the above code:
from m1 Inside static block Value of a : 20 from main
Static methods can call another static methods or variables using the classname. They can not call an instance method or variable.
Instance methods can call static methods or variables using a className or reference variable. They can call another instance method or variable using instance variables.
The Final Keyword
When a final keyword is marked static, you can not override it. In Java, the final keyword is used to indicate that a variable, method, or class cannot be modified or extended.When a variable is declared as final, its value cannot be changed once it has been initialized.
Designing static methods and fields
Static methods do not require an instance of a class. They are shared among all users of the class. There is only one copy of the code for the instance methods. Each instance of a class can call it as many times as it would like.
when a class loads for the first time the static variables will be given memory. For static variable methods, we use the className instead of the object eg className.variable
Having static methods eliminates the need for the caller to instantiate the object just to call the method. You can also use an instance of an object to call a static method. The compiler will check for the type of the reference and use that instead of the object.
If you make a static reference to a non-static method, you will get a Compiler error.
Only instance methods can call other instance methods on the same class without using a reference variable. Instance methods do require an object.
Key takeaways:
- An instance method can call a static method
- A static method can not call an instance method
- Static variables can not use instance variables
- Constant variables (status variables) are meant to never change and use the final specifier to make sure the variable never changes.
Final variables can be reassigned when the statickeyword is the first assignment eg If we declare a static final variable, it means we can initialize it exactly once in a static block. If a final variable is declared and never initialised, we get a compiler error.
Feel free to add your knowledge about Static and Instance variables in the comments below.
The above is the detailed content of Static variables in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Start Spring using IntelliJIDEAUltimate version...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
