What is the differance if we make variable or method static in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the differance if we make variable or method static in java?

it is give us an additional advantage we can call the variable or method by the name of class followed by dot followed by the name of variable of method and if we make an abstract class we can not create an object so we can not using the variable or method so we make it static to call it by easy way

24th Dec 2016, 5:40 AM
aya ahmed
aya ahmed - avatar
2 Answers
+ 2
yes thank you that is so true because the static it self at execution time it is carryout in different place that object caryout in it so if we want to now how many object created from class person as you say in the example we define a static variable and make it ++ in the constructor of the class so at each time we create object the variable is increment and we know it thank you for your illustrations 😊
30th Dec 2016, 8:16 PM
aya ahmed
aya ahmed - avatar
+ 1
The real meaning of a static attribute or method is that they are in the class level. We use a class to create objects. You can create any number of object from that class. But if you want to keep a common property to all of your created object, you will have to use the static variable. As an example take a person class. Users will create numbers of persons. if you want to count how much persons have been created, you cannot use a instant variable in the person class. You have to use a static variable because the person counter is a common factor to person objects. I gave you a brief introduction. Thanks.
30th Dec 2016, 1:25 PM
Viran Malaka
Viran Malaka - avatar