开发者

Parameterless methods/static

开发者 https://www.devze.com 2022-12-17 09:35 出处:网络
As my name suggests, I am a .NET developer but I have a growing interest in Java, and I am interested in learning more about other languages as this helps me to learn more about programming generally.

As my name suggests, I am a .NET developer but I have a growing interest in Java, and I am interested in learning more about other languages as this helps me to learn more about programming generally.

Anyway, my question is this:开发者_StackOverflow社区 Methods which don't take parameters/work with state (which is just parameters in the method, correct me if I am wrong) are recommended to be made static. What is the relationship/link between static and parameterless methods? Not working with state means if you pass a Person object into the method, and you don't edit that object's state (Eg its properties) - this is my understanding.

I don't mind any Java specific answers.

Thanks


"What is the relationship/link between static and parameterless methods? "

None.

"Methods which don't take parameters/work with state... are recommended to be made static"

Really? By whom? Can you provide a link or quote?

Static means that the method belongs to the class -- as a whole -- not any specific object of that class. Therefore, static methods can only deal with static variables, not instance variables.

Parameterless doesn't mean anything. It may be that the method deals only with instance variables or only with static variables. Or it returns a constant. Or it has some calculation which is private to that method. It could, for example, create a socket, do a read using HTTP, and destroy the socket. No parameters; no instance variables.


There is no relationship between static and parameterless methods.

A static method is one which does not access instance state in the receiving class (and therefore does not need to be associated with a particular instance). It can easily take parameters:

public class Calculator
{
  public static int Add(int a, int b) { return a + b; }  // does not need any Calculator state
}

A static method can access its parameters (and can therefore modify their state if they allow it):

public class Officialdom
{
  public static void Rename(Person person) { person.Name = "Bob"; }  // does not need any Officialdom state
}

Conversely, a parameterless method might well need to access receiver state, and therefore be an instance (non-static) method:

public class Spline
{
  private bool _isReticulated;
  public void Reticulate()
  {
    _isReticulated = true;  // does need Spline state
  }
}

(I've posted code samples in C# because this is language-independent; the same notions and distinctions apply in Java, possibly with a few keyword changes.)


there is no connection between static methods and what they do with parameters passed into them. static methods are CLASS level methods and not INSTANCE level in Java. static methods are associated with the Class they are declared in and not instances of those classes.


There's a general principle that methods should not have access to more data than they need. This is one of the reasons why member variables are usually private and OO uses encapsulation to hide data and code from other parts of the system.

When you have a function which does not require access to the variables in that class, some people recommend making the method static.

Whether or not a function has parameters does not affect whether it has access to instance methods.

0

精彩评论

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

关注公众号