0

Java help for “instances” and the meaning of “static”

What exactly are Instances?

2nd Feb 2020, 6:23 AM
Henry Genesis
2 Answers
+ 5
Instances are nothing but objects. An instance variable is a variable that is defined inside the class without the 'static' keyword and defines the state of the object, whereas methods define the behaviour. Static variables on the other hand are class variables which are allocated memory only once because it is shared by all the objects of the class. Also they retain their value during method calls and will always display the final updated value.
2nd Feb 2020, 6:35 AM
Avinesh
Avinesh - avatar
+ 2
Imagine the java class as a blueprint, a design that describes some data (variables) and behavior (methods). When you create an instance of the class (with the "new" keyword), java allocates some memory to a concrete realization of the class. You can make multiple instances, and each of them can have their own separate variables. But static keyword means that the variable or method belongs to the whole class (the blueprint) and shared across all instances. The main() method must always be static. Don't worry if this doesn't make a lot of sense yet. Continue with the Java lessons and you will get it by examples, there will be a chapter about static.
2nd Feb 2020, 8:43 AM
Tibor Santa
Tibor Santa - avatar