What can't I just set everything as static? [closed]
I was just wondering why I can not just set everything as static? I think if I set something as a static then it has a better access time than other variables that haven't set as static.. But I assume there might be a problem and that is the reason why people are not doing this.. What are the problem could be? Thanks in advance..
Consider a class Person
and all your friends are instances of that class. Now consider they all have names, their names are their attributes.
Now, if that name attribute was static, all friends would have the same name.
That's why we have instance variables too.
If you make everything static, then there only exists a single copy of it in the entire program. This can be a problem when you want multiple computations of the same thing to go on in parallel or if you want to track multiple copies of the same logical object at the same time.
As for the access time, one should consider correctness first and foremost, then optimize later. Additionally, optimizations should be based on actual measurements, not speculation. If you actually measure how long things are taking, that is pretty much never going to be the optimization you make.
精彩评论