Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Method (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Special methods== Special methods are very language-specific and a language may support none, some, or all of the special methods defined here. A language's compiler may automatically generate default special methods or a programmer may be allowed to optionally define special methods. Most special methods cannot be directly called, but rather the compiler generates code to call them at appropriate times. ===Static methods=== {{see also|Static member function}} Static methods are meant to be relevant to all the instances of a class rather than to any specific instance. They are similar to [[static variable]]s in that sense. An example would be a static method to sum the values of all the variables of every instance of a class. For example, if there were a <code>Product</code> class it might have a static method to compute the average price of all products. A static method can be invoked even if no instances of the class exist yet. Static methods are called "static" because they are resolved at [[compile time]] based on the class they are called on and not dynamically as in the case with instance methods, which are resolved polymorphically based on the runtime type of the object. ====Examples==== =====In Java===== In Java, a commonly used static method is: Math.max(double a, double b) This static method has no owning object and does not run on an instance. It receives all information from its arguments.<ref name=":0">{{Cite book|title = Clean Code: A Handbook of Agile Software Craftsmanship|last = Martin|first = Robert C.|publisher = Prentice Hall|year = 2009|isbn = 978-0-13-235088-4|pages = 296|author-link = Robert Cecil Martin}}</ref> ===Copy-assignment operators=== Copy-assignment operators define actions to be performed by the compiler when a class object is assigned to a class object of the same type. ===Operator methods=== Operator methods [[Operator overloading|define or redefine operator symbols]] and define the operations to be performed with the symbol and the associated method parameters. C++ example: <syntaxhighlight lang="cpp"> #include <string> class Data { public: bool operator<(const Data& data) const { return roll_ < data.roll_; } bool operator==(const Data& data) const { return name_ == data.name_ && roll_ == data.roll_; } private: std::string name_; int roll_; }; </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)