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
62
votes
8 answers

How to reload a module's function in Python?

Following up on this question regarding reloading a module, how do I reload a specific function from a changed module? pseudo-code: from foo import bar if foo.py has changed: reload bar
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
62
votes
6 answers

Static Method of a Static Class vs. Static Method of a Non-Static Class ( C# )

I was asked the above question in an interview. Could you please explain the differences? ( performance - memory - usage - when to use which ? ) Thank you, Erkan
Erkan Y.
  • 601
  • 1
  • 6
  • 14
61
votes
2 answers

How to properly document S4 methods using roxygen2

I've seen some discussions in SO and other places regarding how this should be or will be done in future versions of Roxygen2. However, I am stuck. How should I go about documenting a S4 generic, as well as its methods, using Roxygen2? A working…
Paul 'Joey' McMurdie
  • 7,295
  • 5
  • 37
  • 41
61
votes
8 answers

How do I change file extension with javascript

Does anyone know an easy way to change a file extension in Javascript? For example, I have a variable with "first.docx" but I need to change it to "first.html".
Joseandro Luiz
  • 8,744
  • 4
  • 20
  • 20
61
votes
3 answers

A method with an optional parameter

Is there a way to make a method that can accept a parameter, but can also be called without one, in which case the parameter is regarded nil like the following? some_func(variable) some_func
TamRock
  • 1,490
  • 1
  • 11
  • 27
61
votes
1 answer

Calling one method from another within same class in Python

I was trying to pass a value from one method to another within the class. Because in my code, "if" is calling class's method on_any_event that in return should call my another method dropbox_fn, which make use of the value from on_any_event. Will it…
kirti
  • 649
  • 1
  • 6
  • 5
61
votes
3 answers

How to call Type Methods within an instance method

Apple has a nice explanation of Type (Class) Methods, however, their example looks like this: class SomeClass { class func someTypeMethod() { // type method implementation goes here } } SomeClass.typeMethod() I see this exact same…
Chris Marshall
  • 4,910
  • 8
  • 47
  • 72
60
votes
10 answers

Method Within A Method

I am creating a C# library with some reusable code and was trying to create a method inside a method. I have a method like this: public static void Method1() { // Code } What I would like to do is this: public static void Method1() { public…
Bali C
  • 30,582
  • 35
  • 123
  • 152
59
votes
3 answers

ReactJS - Call One Component Method From Another Component

I have two components. I want to call a method of the first component from the second component. How can I do it? Here is my code. First Component class Header extends React.Component{ constructor(){ super(); } checkClick(e,…
Kushal Jain
  • 3,029
  • 5
  • 31
  • 48
59
votes
4 answers

How to achieve method chaining in Java?

I want to achieve method chaining in Java. How can I achieve it? Also let me know when to use it. public class Dialog { public Dialog() { } public void setTitle(String title) { //Logic to set title in dialog } …
Bug
  • 625
  • 1
  • 5
  • 7
59
votes
8 answers

Method vs Functions, and other questions

With respect to JS, what's the difference between the two? I know methods are associated with objects, but am confused what's the purpose of functions? How does the syntax of each of them differ? Also, what's the difference between these 2…
Karan Goel
  • 1,117
  • 1
  • 12
  • 24
58
votes
1 answer

Get method's arguments?

I can check all available methods for an object like so: $methods = get_class_methods($object); But how can I see which arguments have to be sent to these methods? Is there a function for this?
Rakward
  • 1,657
  • 2
  • 18
  • 24
57
votes
7 answers

Java: Ternary with no return. (For method calling)

I was wondering if it was possible to do a ternary operation but without returning anything. If it's not possible in Java is it possible in other languages, if so which ones apply? name.isChecked() ? name.setChecked(true):name.setChecked(false);
TylerKinkade
  • 858
  • 2
  • 8
  • 15
57
votes
3 answers

Can I get all methods of a class?

Suppose that I have a .class file, can I get all the methods included in that class ?
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
57
votes
5 answers

Python, Overriding an inherited class method

I have two classes, Field and Background. They look a little bit like this: class Field( object ): def __init__( self, a, b ): self.a = a self.b = b self.field = self.buildField() def buildField( self ): …
d00nut
  • 673
  • 1
  • 5
  • 6