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

Compare two objects with .equals() and == operator

I constructed a class with one String field. Then I created two objects and I have to compare them using == operator and .equals() too. Here's what I've done: public class MyClass { String a; public MyClass(String ab) { a = ab; …
Fastkowy
  • 1,285
  • 3
  • 15
  • 16
90
votes
2 answers

Is there still a use for inline?

I believed, inline was obsolete because I read here: No matter how you designate a function as inline, it is a request that the compiler is allowed to ignore: the compiler might inline-expand some, all, or none of the places where you call a…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
88
votes
12 answers

java how expensive is a method call

I'm a beginner and I've always read that it's bad to repeat code. However, it seems that in order to not do so, you would have to have extra method calls usually. Let's say I have the following class public class BinarySearchTree
jhlu87
  • 3,999
  • 8
  • 38
  • 48
88
votes
3 answers

VueJS accessing a method from another method

I'm using VueJS to make a simple enough resource management game/interface. At the minute I'm looking to activate the roll function every 12.5 seconds and use the result in another function. At the moment though I keep getting the following…
Jackanapes
  • 1,023
  • 1
  • 8
  • 12
86
votes
8 answers

What does the return keyword do in a void method in Java?

I'm looking at a path finding tutorial and I noticed a return statement inside a void method (class PathTest, line 126): if ((x < 0) || (y < 0) || (x >= map.getWidthInTiles()) || (y >= map.getHeightInTiles())) { return; } I'm a novice at Java.…
Relequestual
  • 11,631
  • 6
  • 47
  • 83
86
votes
5 answers

Large difference in speed of equivalent static and non static methods

In this code when I create an Object in the main method and then call that objects method: ff.twentyDivCount(i)(runs in 16010 ms) , it runs much faster than calling it using this annotation: twentyDivCount(i)(runs in 59516 ms). Of course, when I run…
Stabbz
  • 768
  • 5
  • 12
86
votes
6 answers

Do PHP interfaces have properties?

Do interfaces in PHP have properties, or do they only have methods?
never_had_a_name
  • 90,630
  • 105
  • 267
  • 383
86
votes
7 answers

Remove key from dictionary in Python returning new dictionary

I have a dictionary d = {'a':1, 'b':2, 'c':3} I need to remove a key, say c and return the dictionary without that key in one function call {'a':1, 'b':2} d.pop('c') will return the key value - 3 - instead of the dictionary. I am going to need one…
Xeos
  • 5,975
  • 11
  • 50
  • 79
85
votes
5 answers

Whats the difference of functions and methods in Go?

I am trying to get started with Go and the documentation is very good. What I did not find in the documentation is the difference between functions and methods. As far as I understand at the moment: functions are "global", which means I do not have…
Dominik Obermaier
  • 5,610
  • 4
  • 34
  • 45
84
votes
7 answers

How to log exceptions in Java?

There's a common problem I've come across a few times when logging exceptions in Java. There seem to be various different types to deal with. E.g. some wrap other exceptions and some don't have a message at all - only a type. Most code I've seen…
Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
84
votes
4 answers

Can a method be used as an array_map function

I want to do something like this: class Cls { function fun($php) { return 'The rain in Spain.'; } } $ar = array(1,2,3); $instance = new Cls(); print_r(array_map('$instance->fun', $ar)); // ^ this won't work but the first…
allyourcode
  • 21,871
  • 18
  • 78
  • 106
82
votes
9 answers

What is a "method" in Python?

Can anyone, please, explain to me in very simple terms what a "method" is in Python? The thing is in many Python tutorials for beginners this word is used in such way as if the beginner already knew what a method is in the context of Python. While I…
brilliant
  • 2,805
  • 11
  • 39
  • 57
82
votes
2 answers

How would I cross-reference a function generated by autodoc in Sphinx?

I am using the Sphinx autodoc feature to generate documentation based on the docstrings of my Python library. The syntax for cross referencing is found here A label must precede the section in order to allow that section to be referenced from other…
Matthew Stamy
  • 1,114
  • 1
  • 7
  • 9
82
votes
9 answers

What does this boolean "(number & 1) == 0" mean?

On CodeReview I posted a working piece of code and asked for tips to improve it. One I got was to use a boolean method to check if an ArrayList had an even number of indices (which was required). This was the code that was suggested: private static…
Andrew Martin
  • 5,619
  • 10
  • 54
  • 92
81
votes
6 answers

Read the value of an attribute of a method

I need to be able to read the value of my attribute from within my Method, how can I do that? [MyAttribute("Hello World")] public void MyMethod() { // Need to read the MyAttribute attribute and get its value }
Coppermill
  • 6,676
  • 14
  • 67
  • 92