When to make a method static?

2020-05-11 10:16发布

I'd like to know how people decide whether to define a method as static. I'm aware that a method can only be defined as static if it doesn't require access to instance fields. So let's say we have a method that does not access instance fields, do you always define such a method as static, or only if you need to call it statically (without a reference to an instance).

Perhaps another way of asking the same question is whether you use static or non-static as the default?

7条回答
狗以群分
2楼-- · 2020-05-11 11:18

Non static by default, static when I need the functionality to be available from at least two different classes, and I don't want to waste a constructor.

ps. Archimedes rules!

查看更多
登录 后发表回答