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
7724
votes
91 answers

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

I always thought Java uses pass-by-reference. However, I read a blog post which claims that Java uses pass-by-value. I don't think I understand the distinction the author is making. What is the explanation?
user4315
  • 4,775
  • 5
  • 23
  • 9
7585
votes
41 answers

var functionName = function() {} vs function functionName() {}

I've recently started maintaining someone else's JavaScript code. I'm fixing bugs, adding features and also trying to tidy up the code and make it more consistent. The previous developer used two ways of declaring functions and I can't work out if…
Richard Garside
  • 87,839
  • 11
  • 80
  • 93
2098
votes
28 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
  • 25,122
  • 7
  • 21
  • 11
895
votes
13 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
  • 13,245
  • 12
  • 58
  • 96
842
votes
19 answers

Adding a method to an existing object instance in Python

How do I add a method to an existing object (i.e., not in the class definition) in Python? I understand that it's not generally considered good practice to do so, except in some cases.
akdom
  • 32,264
  • 27
  • 73
  • 79
758
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
  • 315,713
  • 212
  • 479
  • 689
653
votes
12 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
  • 10,605
  • 5
  • 22
  • 13
572
votes
7 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
  • 5,937
  • 3
  • 16
  • 11
533
votes
23 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
  • 54,530
  • 68
  • 182
  • 238
527
votes
8 answers

How to pass a function as a parameter in Java?

In Java, how can one pass a function as an argument to another function?
Jason
  • 5,451
  • 4
  • 19
  • 12
494
votes
11 answers

"TypeError: method() takes 1 positional argument but 2 were given" but I only passed one

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
  • 56,143
  • 27
  • 150
  • 160
463
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
  • 177,705
  • 43
  • 90
  • 110
393
votes
4 answers

X does not implement Y (... method has a pointer receiver)

There are already several Q&As on this "X does not implement Y (... method has a pointer receiver)" thing, but to me, they seems to be talking about different things, and not applying to my specific case. So, instead of making the question very…
xpt
  • 20,363
  • 37
  • 127
  • 216
367
votes
18 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,745
  • 6
  • 26
  • 25
356
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,123
  • 2
  • 14
  • 10
1
2 3
99 100