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
104
votes
5 answers

How do I set and access attributes of a class?

Suppose I have this code: class Example(object): def the_example(self): itsProblem = "problem" theExample = Example() print(theExample.itsProblem) When I try it, I get an error that says: Traceback (most recent call last): File…
Adam
  • 1,061
  • 2
  • 8
  • 6
103
votes
2 answers

How does a Python set([]) check if two objects are equal? What methods does an object need to define to customise this?

I need to create a 'container' object or class in Python, which keeps a record of other objects which I also define. One requirement of this container is that if two objects are deemed to be identical, one (either one) is removed. My first thought…
Ada
  • 1,746
  • 4
  • 15
  • 15
103
votes
6 answers

Simple way to reorder methods of a Java class in IntelliJ?

Is there a simpler way of reordering methods within a class source file in IntelliJ than cutting and pasting the code manually? Nowadays I often need this while refactoring legacy code, e.g. to move related methods close to each other in the source…
Péter Török
  • 114,404
  • 31
  • 268
  • 329
101
votes
12 answers

How to get an object's methods?

Is there a method or propertie to get all methods from an object? For example: function foo() {} foo.prototype.a = function() {} foo.prototype.b = function() {} foo.get_methods(); // returns ['a', 'b']; UPDATE: Are there any method like that in…
thom
  • 1,011
  • 2
  • 7
  • 4
101
votes
7 answers

Is it possible to have Methods inside Methods?

I have a method inside of a method. The interior method depends on a variable loop that is being run. Is that a bad idea?
Trip
  • 26,756
  • 46
  • 158
  • 277
101
votes
17 answers

Overriding vs Hiding Java - Confused

I'm confused on how overriding differs from hiding in Java. Can anyone provide more details on how these differ? I read the Java Tutorial but the sample code still left me confused. To be more clear, I understand overriding well. My issue is that I…
Lostlinkpr
  • 1,453
  • 2
  • 12
  • 13
99
votes
5 answers

Define a method outside of class definition?

class MyClass: def myFunc(self): pass Can I create MyFunc() outside of the class definition, maybe even in another module?
user975135
99
votes
19 answers

How to call a method daily, at specific time, in C#?

I've searched on SO and found answers about Quartz.net. But it seems to be too big for my project. I want an equivalent solution, but simpler and (at best) in-code (no external library required). How can I call a method daily, at a specific time? I…
Delta76
  • 13,931
  • 30
  • 95
  • 128
97
votes
7 answers

How to get all methods of a Python class with given decorator?

How to get all methods of a given class A that are decorated with the @decorator2? class A(): def method_a(self): pass @decorator1 def method_b(self, b): pass @decorator2 def method_c(self, t=5): pass
kraiz
  • 2,178
  • 1
  • 22
  • 22
97
votes
9 answers

Can two Java methods have same name with different return types?

Can two Java methods have the same name with different return type? The return type of the methods are different and they are declared with the same method's name. Is that allowed?
jenifer
  • 971
  • 1
  • 6
  • 3
96
votes
6 answers

Calling a method on the main thread?

First of all I am writing code for iphone. I need to be able to call a method on the main thread without using performSelectorOnMainThread. The reason that I don't want to use performSelectorOnMainThread is that it causes problem when I am trying to…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
96
votes
4 answers

How to override to_json in Rails?

Update: This issue was not properly explored. The real issue lies within render :json. The first code paste in the original question will yield the expected result. However, there is still a caveat. See this example: render :json => current_user is…
maček
  • 76,434
  • 37
  • 167
  • 198
95
votes
5 answers

How to use Functions of another File in Dart / Flutter?

I have a Flutter app where I'm using the flutter_web_view package. I'm using it over several different files and would love to create its own file and simply reference with the _launchwebview function anywhere in my app because there are several…
Charles Jr
  • 8,333
  • 15
  • 53
  • 74
91
votes
6 answers

Why does this Kotlin method have enclosing backticks?

What are the backticks used for in the snippet below? Why add them around the fun is(amount:Int ):Boolean { ... }? verifier.`is`(amount)
LF00
  • 27,015
  • 29
  • 156
  • 295
91
votes
4 answers

What is Vue way to access to data from methods?

I have the following code: { data: function () { return { questions: [], sendButtonDisable: false, } }, methods: { postQuestionsContent: function () { var that = this; that.sendButtonDisable = true; …
Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145