开发者

What can't I just set everything as static? [closed]

开发者 https://www.devze.com 2023-03-16 23:01 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be r开发者_Python百科eopened, visit the help center. Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号