• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of Static Variable  RSS feed

 
Mayu Mayooresan
Ranch Hand
Posts: 37
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand how to declare a static variable and when it is declared it is common among all the instances created from the class. I've wrote some exmples myself and tested and i'm happy with the outcome.

But wot bothers me is, the real world usage of the static variable, methods. My SCJP lecturer told me family name is a good example. As it is common among the family. But wot if someone in the family got married and chagned the family name, it'll change the family name of all the others rite

Can someone correct me please.

Thanks.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed a family name is not a good use of a static variable.

A better example: you have a class Car and you want to record the number of Cars that is made by your application.
Then you could increment a static variables each time a Car is made. Counting the number of Cars that is made.

(Of course there are other ways to accomplish the same but that will always be the case)
 
Mayu Mayooresan
Ranch Hand
Posts: 37
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:Indeed a family name is not a good use of a static variable.

A better example: you have a class Car and you want to record the number of Cars that is made by your application.
Then you could increment a static variables each time a Car is made. Counting the number of Cars that is made.

(Of course there are other ways to accomplish the same but that will always be the case)


Now this makes sense. Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!