Questions tagged [methods]

A method is a block of code that performs a task and is associated with a class or an object. It is related to the non-object-oriented concepts of functions and procedures.

A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments.

In object-oriented programming, a method is a subroutine (or procedure) associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time.

Methods have the special property that at runtime, they have access to data stored in an instance of the class (or class instance or class object or object) they are associated with and are thereby able to control the state of the instance.

The association between class and method is called binding. A method associated with a class is said to be bound to the class. Methods can be bound to a class at compile time (static binding) or to an object at runtime (dynamic binding).

Common features

They have few things in common:

  1. They may take some parameters / arguments for their processing.
  2. They may have a return-value, that returns some value to the calling method.
  3. The concept of and is also associated with methods.

References

Also see:

30291 questions
214
votes
11 answers

Can I invoke an instance method on a Ruby module without including it?

Background: I have a module which declares a number of instance methods module UsefulThings def get_file; ... def delete_file; ... def format_text(x); ... end And I want to call some of these methods from within a class. How you normally do…
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
212
votes
5 answers

Final arguments in interface methods - what's the point?

In Java, it is perfectly legal to define final arguments in interface methods and do not obey that in the implementing class, e.g.: public interface Foo { public void foo(int bar, final int baz); } public class FooImpl implements Foo { …
mindas
  • 26,463
  • 15
  • 97
  • 154
200
votes
8 answers

What does .class mean in Java?

What does .class mean in Java? For example, if I created a class called Print. What does Print.class return?
Quinn Wei
  • 2,183
  • 3
  • 14
  • 12
197
votes
9 answers

What's a "static method" in C#?

What does it mean when you add the static keyword to a method? public static void doSomething(){ //Well, do something! } Can you add the static keyword to class? What would it mean then?
Moshe
  • 57,511
  • 78
  • 272
  • 425
194
votes
6 answers

How do I pass multiple parameters in Objective-C?

I have read several of the post about Objective-C method syntax but I guess I don't understand multiple names for a method. I'm trying to create a method called getBusStops with NSString and NSTimeInterval parameters and a return type of…
Atma
  • 29,141
  • 56
  • 198
  • 299
188
votes
10 answers

How to name factory like methods?

I guess that most factory-like methods start with create. But why are they called "create"? Why not "make", "produce", "build", "generate" or something else? Is it only a matter of taste? A convention? Or is there a special meaning in…
deamon
  • 89,107
  • 111
  • 320
  • 448
185
votes
8 answers

Are there any Java method ordering conventions?

I've got a large-ish class (40 or so methods) that is part of a package I will be submitting as course-work. Currently, the methods are pretty jumbled up in terms of utility public/private etc. and I want to order them in a sensible way. Is there a…
fredley
  • 32,953
  • 42
  • 145
  • 236
184
votes
5 answers

How to check whether an object has certain method/property?

Using dynamic pattern perhaps? You can call any method/property using the dynamic keyword, right? How to check whether the method exist before calling myDynamicObject.DoStuff(), for example?
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
180
votes
4 answers

Method Syntax in Objective-C

Can someone explain this method declaration syntax for me? In this function, the number of rows of a UIPickerView (slot machine UI on the iPhone) is being returned. From my understanding, the Method is called 'pickerView', and returns an…
Craig
  • 16,291
  • 12
  • 43
  • 39
180
votes
21 answers

When is a Java method name too long?

In the last weeks I've seen some guys using really long names for a Method or Class (50 characters), this is usually under the premise that it improves readability, my opinion is that a long name like this is an indicator that we are trying to do a…
MexicanHacker
  • 2,685
  • 3
  • 19
  • 20
180
votes
12 answers

Can overridden methods differ in return type?

Can overridden methods have different return types?
santidoo
  • 1,913
  • 2
  • 12
  • 10
177
votes
16 answers

Properties vs Methods

Quick question: When do you decide to use properties (in C#) and when do you decide to use methods? We are busy having this debate and have found some areas where it is debatable whether we should use a property or a method. One example is…
Sir Rippov the Maple
  • 7,491
  • 5
  • 42
  • 52
177
votes
16 answers

Python __call__ special method practical example

I know that __call__ method in a class is triggered when the instance of a class is called. However, I have no idea when I can use this special method, because one can simply create a new method and perform the same operation done in __call__ method…
mohi666
  • 6,842
  • 9
  • 45
  • 51
177
votes
6 answers

Java variable number of arguments for a method

Is it possible to declare a method that will allow a variable number of parameters ? What is the symbolism used in the definition that indicate that the method should allow a variable number of parameters? Answer: varargs
user69514
  • 26,935
  • 59
  • 154
  • 188
170
votes
7 answers

Passing just a type as a parameter in C#

Hypothetically it'd be handy for me to do this: foo.GetColumnValues(dm.mainColumn, int) foo.GetColumnValues(dm.mainColumn, string) where the GetColumns method will call a different method inside depending on the type passed. Yes, I could do it as a…
Mark Mayo
  • 12,230
  • 12
  • 54
  • 85