DEV Community

Mohamed Ajmal
Mohamed Ajmal

Posted on

Java variables and Objects.

Static variables: These are variables that are shared among all the instances of a class.

Non-static variables: These are variables that belong to each individual instance of a class.

Java Static Variables : When a variable is declared as static, then a single copy of the variable is created and shared among all objects at a class level. Static variables are essentially, global variables.

Java Non-static variables : Non-static variables are variables that belongs to a specified object of a class, it is also known as instance variable.
These variables are declared outside of a method, constructor or block. Each object created from the class gets its own separate copy of these variables.

Ref : https://www.geeksforgeeks.org/java/difference-between-static-and-non-static-variables-in-java/

Difference between static and non-static:

OBJECTS

In Java, an object is an instance of a class, created using the new keyword. The class acts as a blueprint, defining the object's attributes (state) and methods (behavior), while the object is the concrete entity with specific values in memory.

State: Represented by attributes (variables) that store data.

Behavior: Represented by methods (functions) that define the actions an object can perform.

Identity: A unique name or identifier that distinguishes one object from another, even if they have the same state and behavior.

Top comments (0)