• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it good to make more variable static in java?  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I have a question in mind that.We can declare any member of a class static or non-static.The Question is it good to make more member variables static or not?

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is not correct. It is not about good or bad.
It is about do you need that or not? if not, then nothing good to create "more" static variables than you need.


 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you think about static versus non-static, think of whether you need multiply copies of a member for each instance of a class. For instance, it you are using pi in a class, that would obviously be static (and final), because pi isn't going to vary from instance to instance (object to object). But if you have a Car class, using a static speed member would mean every Car instance would have the same speed! So it should be non-static.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!