Talk:Method (computer programming)
In use, a method is a function.. Fundamentally, Methods + Data = Object.. Just the elaborations of this invariant property of objects has muddied the definition of what an object is.. For instance inheritance is more a language construct than a property of an object.. Also a "Class" is the beginning state of an object, so what use is a class aside from an object, as objects could be used to make more objects.. I think over the years Java and C++ has muddied the definition of what an object is, when really its a simple concept that I believe (in conspiracy theory) that capitalists not interested in the adoption of object oriented languages has intentionally sabotaged to avoid the competition from perpetually consistent pluggable entities. Agents are merely mobile objects and whatever happened to the use of Agents? I challenge you to track down the use of Agents, popular in the 90s but was this just a fad? You may find they were systematically extinguished (I was told one such language that used agents as objects was forced out indirectly by Microsoft, but this is a rumor). --Rofthorax 10:38, 24 August 2005 (UTC)
Is static really the same as "shared"--I thought it was the opposite--static *fields* cannot be modified.
more information and more accurate information needs to be put here. I don't know enough about Java to help.
My references: (by memory)
Java Methods, avaliable directly from www.skylit.com
The static keyword is, by default, related to single storage. For instance, any modification of a static field will be seen by all instances of a given class as they are applied to the same piece of memory. There are exceptions, however. I will care to provide one example: global variables in C which are also static will have internal linkage, that is, they will only be seen by functions inside the same file (if, naturally, placed after the variable in question was defined).
The keyword global is related to access. A global variable is one which will be accessed by all functions which do not include definition of a variable of the same name and type. Global variables will naturally have a single piece of memory related to them. This means they are "static" in such sense. But a language may allow them to be defined as static and this will have a different behavior, as stated on the above paragraph.18.104.22.168 18:49, 18 January 2007 (UTC)
I would not consider static functions as methods [they're just functions associated with the class]. Similarly, constructors and destructors (in C++) are not strictly speaking methods. A method should be able to assume that invariants are satisfied on entry to the method, and should leave the object in a valid state. [unsigned]
Subroutines and Methods
- A subroutine is one possible way to implement a method. Wouter Lievens 14:33, 26 January 2007 (UTC)
"Method" is just a new word for function or procedure that has become popular in the object oriented paradigm. This article should refer to existing articles on functions and procedures, should relate methods to functions and procedures, and should explain in very clear terms that a method is simply a module of code that contains a sequence of instructions. Some methods attach to classes, others to objects, others to neither (in structured programming languages). There should be some explanation of parameters and return values, but nothing that duplicates discussions already in place under "function" or "procedure" or the more generic "subroutine".
Actually it's wrong to say that "method is just a new word for function or procedure." When I invoke a procedure, I can be sure exactly what code will be called. When I invoke a method of an object, the actual code will vary according to the class of the object (which might be different from what I expect.) (Erik Zoltan (talk) 16:33, 16 September 2008 (UTC))
It's true that a method invocation might execute one out of several polymorphic pieces of code. I would happily describe each polymorphic variation as a "method" - that is, a function or procedure. Are you implying that the word "method" applies only to the polymorphic signature, and a different word should be used to describe each of the implementations? If so, what word? Paul Foxworthy (talk) 04:08, 17 September 2008 (UTC)
Note that there already exists a definition on page Dynamic_dispatch;
dynamic dispatch is the process of mapping a message to a specific sequence of code (method) at runtime..
Disregarding the particular focus on dynamic dispatch leave us with the definition "dispatch is the process of mapping a message to a specific sequence of code (method)". This implies that the it is the message which is sent, delivered and understood (reacted to). That is, the polymorphic signature, including intended semantics, are captured within the message definition. It is important to clearly distinguish this from the method which contains the implementation details regarding what specifically should happen upon message delivery, with relation to specific object or class (or set of classes in the case of multi-methods). The two concepts are often confused, as well as the difference between methods and procedures as clarified by Erik Zoltan above.
Based on current terminology, I'd offer the general definition that a method is a self contained, specific sequence of code (e.g. a procedure or subroutine) that has been explicitly associated with an object or class and will be automatically selected for use upon delivery of the message it implements. This rules out general procedures, functions, lambdas, case blocks within switch statements (and by extension if-blocks) as well as blocks within for and while loops, which are generally not considered methods (except perhaps just before being extracted into such during refactoring), and rules in Erlang's message handling mechanisms. --Mike Amy 11 Oct 2009
I think that Mike Amy offers a correct definition and explanation above. However for a general audience it is desirable to define method without requiring the use of message which is often misunderstood. I would therefore tweak Mike's definition and say that a method is a subroutine or function explicitly associated with an object or class and leave it at that. Then I could define message, polymorphism and other higher-level concepts by referring to the method without circularity. --Erik Zoltan (talk) 02:04, 28 February 2010 (UTC)
The page as it exists today is unsatisfactory in at least the following respects:
- No mention of dynamic dispatch (thus, article is incomplete)
- No mention of late binding (thus, article is incomplete)
- No mention of polymorphism (thus, article is incomplete)
- Defines methods as "attached" to objects: see CLOS (thus, article is incorrect)
It does not fit in the hierarchy. Java's design is simply silly. This is because Java was designed to be unimplementable, dealing only with objects living... No beginning of the execution was designed. So main() was fit something like randomly. Please stop using java, consider ANY OTHER serious language. DrW —Preceding unsigned comment added by 22.214.171.124 (talk) 20:51, 22 April 2008 (UTC)
Java designed to be "unimplementable"? That must be why it's "unimplemented" to the point of market dominance. Truth is, Java's implementation of
main() is no more random than in C++. In neither language does the main routine fit into the class hierarchy. Both languages hack the OO paradigm in order to give programs a point of entry. I agree with anyone who wants to argue that C++'s enforcement of only one point of entry is cleaner, but to call Java's design "silly" is putting it way too strongly. --Rtperson (talk) 16:55, 10 September 2008 (UTC)
Simon says, "It's static. Show some respect."
I came here for an explanation of the "static" condition and I still don't get what it is, or why it matters, or - and this may be the more acute complaint - why it still matters. I never studied computer science but I program in Java every day and when the compiler jibs at my calling something static from a non-static something, I merely change this or that and the quibble is no more. Why? Why did I have to limbo under a five-foot bar, or jump over a five-inch one? Is there something about silicon, its chemistry or electronic properties, that forces, tortuously but inevitably, critical and profound decisionmaking about when stuff happens? Or is there something about memory management, some "best practice," that encourages if not requires some consideration of this? Or is this a relict of 1940's-vintage theoretical computer science, something that visionaries imagined could be a big deal, only once people managed to build the darn things, it turned out not to matter? But the neurosis (I think it's that, and in the case of Java know it's that) persists by sheer force of tradition?
Many, perhaps most other programming languages make no provision for this. They seem to work fine. I generously suppose the programmer who uses them gets comfortable in a fool's paradise, thus exposing himself to misadventures, disasters the Java compiler would pre-emptively have saved him from. But which disasters? Jahutter (talk) 23:01, 2 August 2012 (UTC)
Use of Main is terrible for a first example
Starting with the first example for a method with something defined on "Main" is an awful example. If you are going to define a method for a rectangle it should be defined on a rectangle object not on Main. The first example really misses the entire point of what OO and methods are supposed to be about. It's more the kind of example I would expect from a conventional programmer who doesn't really understand what methods and OO really are yet. MadScientistX11 (talk) 18:55, 17 December 2013 (UTC)
- I notice the "Main" and rectangle examples are continued in the rest of the article. Oy Vey! I don't like to remove large sections of an article without having something to put in it's place but I'm tempted (but won't for now) to just blank all that stuff. It really gives the wrong idea to anyone coming to this article for an overview of what a method is. "first do no harm" should be the rule I would think, better to say nothing than to give someone a wrong idea and the way methods are defined in this article as it currently stands looks like the examples were developed by someone who doesn't really know how to do OO at all. MadScientistX11 (talk) 15:10, 19 December 2013 (UTC)