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:

27842 questions
7058
votes
89 answers

Is Java "pass-by-reference" or "pass-by-value"?

I always thought Java uses pass-by-reference. However, I've seen a couple of blog posts (for example, this blog) that claim that it isn't (the blog post says that Java uses pass-by-value). I don't think I understand the distinction they're…
user4315
  • 4,725
  • 5
  • 20
  • 9
3949
votes
32 answers

Difference between staticmethod and classmethod

What is the difference between a function decorated with @staticmethod and one decorated with @classmethod?
Daryl Spitzer
  • 121,723
  • 75
  • 151
  • 166
1911
votes
36 answers

What's the difference between a method and a function?

Can someone provide a simple explanation of methods vs. functions in OOP context?
willc2
  • 36,081
  • 24
  • 84
  • 99
1826
votes
25 answers

Does Java support default parameter values?

I came across some Java code that had the following structure: public MyParameterizedFunction(String param1, int param2) { this(param1, param2, false); } public MyParameterizedFunction(String param1, int param2, boolean param3) { //use all…
gnavi
  • 21,966
  • 7
  • 19
  • 11
788
votes
11 answers

Pass Method as Parameter using C#

I have several methods all with the same parameter types and return values but different names and blocks. I want to pass the name of the method to run to another method that will invoke the passed method. public int Method1(string) { // Do…
user31673
  • 11,875
  • 12
  • 55
  • 95
743
votes
8 answers

Is arr.__len__() the preferred way to get the length of an array in Python?

In Python, is the following the only way to get the number of elements? arr.__len__() If so, why the strange syntax?
Joan Venge
  • 269,545
  • 201
  • 440
  • 653
714
votes
17 answers

Adding a Method to an Existing Object Instance

I've read that it is possible to add a method to an existing object (i.e., not in the class definition) in Python. I understand that it's not always good to do so. But how might one do this?
akdom
  • 28,041
  • 24
  • 70
  • 79
572
votes
10 answers

Why are exclamation marks used in Ruby methods?

In Ruby some methods have a question mark (?) that ask a question like include? that ask if the object in question is included, this then returns a true/false. But why do some methods have exclamation marks (!) where others don't? What does it mean?
Lennie
  • 9,525
  • 5
  • 20
  • 13
489
votes
22 answers

Getting the name of the currently executing method

Is there a way to get the name of the currently executing method in Java?
Omar Kooheji
  • 50,943
  • 65
  • 176
  • 234
463
votes
6 answers

Why do some functions have underscores "__" before and after the function name?

This "underscoring" seems to occur a lot, and I was wondering if this was a requirement in the Python language, or merely a matter of convention? Also, could someone name and explain which functions tend to have the underscores, and why (__init__,…
Chuck Testa
  • 4,847
  • 3
  • 14
  • 11
455
votes
18 answers

What is the difference between class and instance methods?

What's the difference between a class method and an instance method? Are instance methods the accessors (getters and setters) while class methods are pretty much everything else?
Devoted
  • 90,341
  • 41
  • 85
  • 110
357
votes
1 answer

List view getListItemXmlAttributes method fails with child publication items

I have created a JS class to populate SG/Folder list view data, when items are modified. (As per Jaime's approach) Everything works great when I operate on items in the publication they're created in. Ex: I open a component or page and the custom…
Warner Soditus
  • 4,133
  • 2
  • 12
  • 10
355
votes
10 answers

TypeError: method() takes 1 positional argument but 2 were given

If I have a class... class MyClass: def method(arg): print(arg) ...which I use to create an object... my_object = MyClass() ...on which I call method("foo") like so... >>> my_object.method("foo") Traceback (most recent call…
Zero Piraeus
  • 47,176
  • 24
  • 135
  • 148
347
votes
17 answers

When is the finalize() method called in Java?

I need to know when the finalize() method is called in the JVM. I created a test class which writes into a file when the finalize() method is called by overriding it. It is not executed. Can anybody tell me the reason why it is not executing?
Rajesh Kumar J
  • 4,495
  • 6
  • 22
  • 25
337
votes
10 answers

How to find where a method is defined at runtime?

We recently had a problem where, after a series of commits had occurred, a backend process failed to run. Now, we were good little boys and girls and ran rake test after every check-in but, due to some oddities in Rails' library loading, it only…
Matt Rogish
  • 22,940
  • 11
  • 72
  • 92
1
2 3
99 100