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
119
votes
10 answers

Calling a function within a Class method?

I have been trying to figure out how to go about doing this but I am not quite sure how. Here is an example of what I am trying to do: class test { public newTest(){ function bigTest(){ //Big Test Here } …
WAC0020
  • 1,199
  • 2
  • 8
  • 3
118
votes
4 answers

Android: How can I pass parameters to AsyncTask's onPreExecute()?

I use an AsyncTask for loading operations that I implemented as an inner class. In onPreExecute() I show a loading dialog which I then hide again in onPostExecute(). But for some of the loading operations I know in advance that they will finish…
Steven Meliopoulos
  • 4,450
  • 4
  • 34
  • 36
117
votes
5 answers

Declaration of Methods should be Compatible with Parent Methods in PHP

Strict Standards: Declaration of childClass::customMethod() should be compatible with that of parentClass::customMethod() What are possible causes of this error in PHP? Where can I find information about what it means to be compatible?
waiwai933
  • 14,133
  • 21
  • 62
  • 86
116
votes
11 answers

Making java method arguments as final

What difference that final makes between the code below. Is there any advantage in declaring the arguments as final. public String changeTimezone( Timestamp stamp, Timezone fTz, Timezone toTz){ return .... } public String changeTimezone(final…
John
  • 2,682
  • 5
  • 23
  • 24
114
votes
7 answers

Naming conventions for Java methods that return boolean

I like using question mark at the end of method/function names in other languages. Java doesn't let me do this. As a workaround how else can I name boolean returning methods in Java? Using an is, has, should, can in the front of a method sound okay…
letronje
  • 9,002
  • 9
  • 45
  • 53
113
votes
21 answers

Should C# methods that *can* be static be static?

Should C# methods that can be static be static? We were discussing this today and I'm kind of on the fence. Imagine you have a long method that you refactor a few lines out of. The new method probably takes a few local variables from the parent…
Bernhard Hofmann
  • 10,321
  • 12
  • 59
  • 78
111
votes
13 answers

Is it possible to overload Python assignment?

Is there a magic method that can overload the assignment operator, like __assign__(self, new_value)? I'd like to forbid a re-bind for an instance: class Protect(): def __assign__(self, value): raise Exception("This is an ex-parrot") var =…
Caruccio
  • 3,489
  • 4
  • 18
  • 14
110
votes
5 answers

What is the purpose of "!" and "?" at the end of method names?

Sometimes I see methods in Ruby that have "?" and "!" at the end of them, e.g: name = "sample_string" name.reverse name.reverse! name.is_binary_data? I was wondering what their purpose is? Are they just syntax sugarcoating?
itsaboutcode
  • 24,525
  • 45
  • 110
  • 156
110
votes
3 answers

Finding # occurrences of a character in a string in Ruby

I'm looking for the Ruby method (1.9...) that can help me find the number of occurrences of a character in a string. I'm looking for all occurrences, not just the first one. For example: "Melanie is a noob" There are two occurrences of the letter…
Melanie Palen
  • 2,645
  • 6
  • 31
  • 50
110
votes
10 answers

How to add a new method to a php object on the fly?

How do I add a new method to an object "on the fly"? $me= new stdClass; $me->doSomething=function () { echo 'I\'ve done something'; }; $me->doSomething(); //Fatal error: Call to undefined method stdClass::doSomething()
MadBad
  • 1,203
  • 2
  • 9
  • 7
108
votes
5 answers

When to use an object instance variable versus passing an argument to the method

How do you decide between passing arguments to a method versus simply declaring them as object instance variables that are visible to all of the object's methods? I prefer keeping instance variables in a list at the end of the Class, but this list…
Ziggy
  • 21,845
  • 28
  • 75
  • 104
108
votes
11 answers

Computed read-only property vs function in Swift

In the Introduction to Swift WWDC session, a read-only property description is demonstrated: class Vehicle { var numberOfWheels = 0 var description: String { return "\(numberOfWheels) wheels" } } let vehicle =…
Stuart
  • 36,683
  • 19
  • 101
  • 139
107
votes
15 answers

array_unique for objects?

Is there any method like the array_unique for objects? I have a bunch of arrays with 'Role' objects that I merge, and then I want to take out the duplicates :)
user89862
106
votes
8 answers

How to detect unused methods and #import in Objective-C

After working a long time on an iPhone app, I realized that my code is quite dirty, containing several #import and methods that are not called or useful at all. I would like to know if there's any compiler directive or way to detect those useless…
Hectoret
  • 3,553
  • 13
  • 49
  • 56
105
votes
6 answers

Inheritance of private and protected methods in Python

I know, there are no 'real' private/protected methods in Python. This approach isn't meant to hide anything; I just want to understand what Python does. class Parent(object): def _protected(self): pass def __private(self): …
uloco
  • 2,283
  • 4
  • 22
  • 37