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

Why does the Scala compiler disallow overloaded methods with default arguments?

While there might be valid cases where such method overloadings could become ambiguous, why does the compiler disallow code which is neither ambiguous at compile time nor at run time? Example: // This fails: def foo(a: String)(b: Int = 42) = a +…
soc
  • 27,983
  • 20
  • 111
  • 215
161
votes
3 answers

What is the "get" keyword before a function in a class?

What does get mean in this ES6 class? How do I reference this function? How should I use it? class Polygon { constructor(height, width) { this.height = height; this.width = width; } get area() { return this.calcArea() } …
Armeen Moon
  • 18,061
  • 35
  • 120
  • 233
161
votes
9 answers

What does 'public static void' mean in Java?

What does public static void mean in Java? I'm in the process of learning. In all the examples in the book I'm working from public static void comes before any method that is being used or created. What does this mean?
David
  • 14,569
  • 34
  • 78
  • 107
160
votes
15 answers

Is it wrong to use Deprecated methods or classes in Java?

I am using eclipse to develop a web application. Just today I have updated my struts version by changing the JAR file. I am getting warnings at some places that methods are deprecated, but the code is working fine. I want to know some things Is it…
Umesh Aawte
  • 4,590
  • 7
  • 41
  • 51
154
votes
14 answers

Why can't I declare static methods in an interface?

The topic says the most of it - what is the reason for the fact that static methods can't be declared in an interface? public interface ITest { public static String test(); } The code above gives me the following error (in Eclipse, at least):…
Henrik Paul
  • 66,919
  • 31
  • 85
  • 96
154
votes
12 answers

How to get instance variables in Python?

Is there a built-in method in Python to get an array of all a class' instance variables? For example, if I have this code: class hi: def __init__(self): self.ii = "foo" self.kk = "bar" Is there a way for me to do this: >>>…
Chris Bunch
  • 87,773
  • 37
  • 126
  • 127
152
votes
12 answers

In Java, how do I call a base class's method from the overriding method in a derived class?

I have two Java classes: B, which extends another class A, as follows : class A { public void myMethod() { /* ... */ } } class B extends A { public void myMethod() { /* Another code */ } } I would like to call the A.myMethod() from…
Creasixtine
  • 740
  • 3
  • 11
  • 33
151
votes
11 answers

Dynamically replace the contents of a C# method?

What I want to do is change how a C# method executes when it is called, so that I can write something like this: [Distributed] public DTask Solve(int n, DEvent callback) { for (int m = 2; m < n - 1; m += 1) if (m % n == 0) …
June Rhodes
  • 3,047
  • 4
  • 23
  • 29
150
votes
8 answers

Java synchronized static methods: lock on object or class

The Java documentation says: It is not possible for two invocations of synchronized methods on the same object to interleave. What does this mean for a static method? Since a static method has no associated object, will the synchronized…
jbu
  • 15,831
  • 29
  • 82
  • 105
146
votes
7 answers

class method generates "TypeError: ... got multiple values for keyword argument ..."

If I define a class method with a keyword argument thus: class foo(object): def foodo(thing=None, thong='not underwear'): print thing if thing else "nothing" print 'a thong is',thong calling the method generates a TypeError: myfoo =…
drevicko
  • 14,382
  • 15
  • 75
  • 97
144
votes
9 answers

Why is a static method considered a method?

I'm writing an explanation for some code for a course, and have been accidentally using the words method and function interchangeably. I decided to go back over and fix the wording, but ran into a hole in my understanding. From what I understand, a…
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
143
votes
7 answers

Git - how do I view the change history of a method/function?

So I found the question about how to view the change history of a file, but the change history of this particular file is huge and I'm really only interested in the changes of a particular method. So would it be possible to see the change history…
Erik B
  • 40,889
  • 25
  • 119
  • 135
142
votes
5 answers

Method names for getting data

Warning: This is a not very serious question/discussion that I am posting... but I am willing to bet that most developers have pondered this "issue"... Always wanted to get other opinions regarding naming conventions for methods that went and got…
Jason
  • 2,806
  • 2
  • 28
  • 38
142
votes
8 answers

Behaviour of final static method

I have been playing around with modifiers with static method and came across a weird behaviour. As we know, static methods cannot be overridden, as they are associated with class rather than instance. So if I have the below snippet, it compiles…
Harish
  • 7,589
  • 10
  • 36
  • 47
142
votes
6 answers

How to bind an unbound method without calling it?

In Python, is there a way to bind an unbound method without calling it? I am writing a wxPython program, and for a certain class I decided it would be nice to group the data of all of my buttons together as a class-level list of tuples, like…
Dan Passaro
  • 4,211
  • 2
  • 29
  • 33