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
56
votes
7 answers

Scala - mutable (var) method parameter reference

EDIT: I keep getting upvotes here. Just for the record, I no longer think this is important. I haven't needed it since I posted it. I would like to do following in Scala ... def save(srcPath: String, destPath: String) { if…
woky
  • 4,686
  • 9
  • 36
  • 44
56
votes
7 answers

Return multiple values from a Java method: why no n-tuple objects?

Why isn't there a (standard, Java certified) solution, as part of the Java language itself, to return multiple values from a Java method, rather than developers having to use their own means, such as Maps, Lists, Pairs, etc.? Why does Java not…
Saket
  • 45,521
  • 12
  • 59
  • 79
56
votes
4 answers

Generic type as parameter in Java Method

Do you think it is possible to create something similar to this? private ArrayList increaseSizeArray(ArrayList array_test, GenericClass) { array_test.add(new GenericObject()); // instance of GenericClass return array_test; }
Pierre Guilbert
  • 5,057
  • 3
  • 19
  • 19
56
votes
6 answers

Difference between methods and functions, in Python compared to C++

I'm doing Code Academy's tutorials on Python, and I'm a bit confused about the definition of method and function. From the tutorial: You already know about some of the built-in functions we've used on (or to create) strings, such as .upper(),…
Q-bertsuit
  • 3,223
  • 6
  • 30
  • 55
55
votes
3 answers

The cost of nested methods

In Scala one might define methods inside other methods. This limits their scope of use to inside of definition block. I use them to improve readability of code that uses several higher-order functions. In contrast to anonymous function literals,…
Palimondo
  • 7,281
  • 4
  • 39
  • 58
54
votes
5 answers

Does Java support inner / local / sub methods?

This is my code. public class SubFunction { private String drawTribleX(){ return trible("X"); } private String trible(String t){ return t + t + t; } public static void main(String[] args){ SubFunction o =…
diewland
  • 1,865
  • 5
  • 30
  • 41
54
votes
4 answers

How to override method via keyboard shortcut in Android Studio

I can finally ask a question to get my points up. In android studio, I want to be able to override the method, however I do not know the keyboard shortcut. Does anyone know?
ahsan.dev
  • 689
  • 1
  • 5
  • 10
54
votes
3 answers

Why private method can not be final as well?

Is it redundant to add private and final to a same method? class SomeClass { //--snip-- private final void doStuff() { // private work here } } If it's private, there's no way anyone can override it, right? Why is it…
MightyPork
  • 18,270
  • 10
  • 79
  • 133
54
votes
3 answers

Which overload will get selected for null in Java?

If I write this line in Java: JOptionPane.showInputDialog(null, "Write something"); Which method will be called? showInputDialog(Component parent, Object message) showInputDialog(Object message, Object initialSelectionValue) I can test it. But in…
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
54
votes
13 answers

Redefine Class Methods or Class

Is there any way to redefine a class or some of its methods without using typical inheritance? For example: class third_party_library { function buggy_function() { return 'bad result'; } function other_functions(){ return…
SeanDowney
  • 17,368
  • 20
  • 81
  • 90
53
votes
3 answers

Why can't you call abstract functions from abstract classes in PHP?

I've set up an abstract parent class, and a concrete class which extends it. Why can the parent class not call the abstract function? //foo.php
Cam
  • 14,930
  • 16
  • 77
  • 128
52
votes
1 answer

How to overload Python's __bool__ method in 2.x?

I thought this should print "False", why is it printing "True"? >>> class Foo(object): ... def __bool__(self): ... return False ... >>> f = Foo() >>> if f: ... print "True" ... else: ... print "False" ... True >>>
dividebyzero
  • 1,243
  • 2
  • 9
  • 17
52
votes
7 answers

How do you create a method for a custom object in JavaScript?

Is it like... var obj = new Object(); obj.function1 = function(){ //code } or something like that?
MetaGuru
  • 42,847
  • 67
  • 188
  • 294
52
votes
4 answers

Calling renderRows() on Angular Material Table

I'm trying to get my Angular Table to refresh after updating the data used in the table. The docs say "you can trigger an update to the table's rendered rows by calling its renderRows() method." but it is not like a normal child component where I…
av0000
  • 1,917
  • 6
  • 31
  • 51
52
votes
4 answers

How to find instance of a bound method in Python?

>>> class A(object): ... def some(self): ... pass ... >>> a=A() >>> a.some > IOW, I need to get access to "a" after being handed over only "a.some".
mrkafk
  • 617
  • 1
  • 5
  • 4