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
75
votes
2 answers

Objective C calling method dynamically with a string

Im just wondering whether there is a way to call a method where i build the name of the method on the fly with a string. e.g. I have a method called loaddata -(void)loadData; to call this i would normally call it like [self loadData]; But i want…
IPadHackAndSlash
  • 955
  • 2
  • 7
  • 7
75
votes
5 answers

print() vs debugPrint() in swift

This might be a simple question but because of clear understanding between print() and debug() print in swift I am unable to understand where to use each one.
Rajan Twanabashu
  • 4,586
  • 5
  • 43
  • 55
75
votes
7 answers

When does it pay off to use S4 methods in R programming

I program regularly in R in a professional context, and I write packages for clients or co-workers as well. Some of the programmers here have a Java background and insist on doing everything the object-oriented way, using S4 methods. My experience…
Joris Meys
  • 106,551
  • 31
  • 221
  • 263
75
votes
6 answers

How to Exit a Method without Exiting the Program?

I am still pretty new to C# and am having a difficult time getting used to it compared to C/CPP. How do you exit a function on C# without exiting the program like this function would? if (textBox1.Text == "" || textBox1.Text == String.Empty ||…
Nightforce2
  • 1,426
  • 5
  • 18
  • 31
75
votes
29 answers

Converting Integers to Roman Numerals - Java

This is a homework assignment I am having trouble with. I need to make an integer to Roman Numeral converter using a method. Later, I must then use the program to write out 1 to 3999 in Roman numerals, so hardcoding is out. My code below is very…
user1752197
  • 781
  • 1
  • 6
  • 4
74
votes
7 answers

How can I create a Java method that accepts a variable number of arguments?

For example, Java's own String.format() supports a variable number of arguments. String.format("Hello %s! ABC %d!", "World", 123); //=> Hello World! ABC 123! How can I make my own function that accepts a variable number of arguments? Follow-up…
maček
  • 76,434
  • 37
  • 167
  • 198
73
votes
2 answers

Java method with unlimited arguments

The Spring framework uses methods where you can pass as many arguments as you like. I would like to write a function that can also take an unlimited amount of data. How is this feature called so that I can read about it? Or how can I define it?
Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
73
votes
5 answers

python: how to get information about a function?

When information about a type is needed you can use: my_list = [] dir(my_list) gets: ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',…
taper
  • 9,236
  • 5
  • 28
  • 29
73
votes
5 answers

includes() not working in all browsers

right here is a block of my code. It works perfect in fireFox and Chrome. But not in IE. I get the error "Object doesn't support property or method 'includes'" function rightTreeSwapfunc2() { if…
73
votes
1 answer

How to make method call another one in classes?

Now I have two classes allmethods.cs and caller.cs. I have some methods in class allmethods.cs. I want to write code in caller.cs in order to call a certain method in the allmethods class. Example on code: public class allmethods public static void…
Mina Hafzalla
  • 2,681
  • 9
  • 30
  • 44
73
votes
4 answers

Advantage of set and get methods vs public variable

Possible Duplicate: Why use getters and setters? Is there any advantage to making methods to access private variables in your class instead of making the variable public? For example is the second case better than the first? //Case 1 public class…
72
votes
3 answers

How do I pass multiple arguments to a ruby method as an array?

I have a method in a rails helper file like this def table_for(collection, *args) options = args.extract_options! ... end and I want to be able to call this method like this args = [:name, :description, :start_date, :end_date] table_for(@things,…
Chris Drappier
  • 5,280
  • 10
  • 40
  • 64
72
votes
3 answers

How to call a method stored in a HashMap? (Java)

I have a list of commands (i, h, t, etc) that the user will be entering on a command line/terminal Java program. I would like to store a hash of command/method pairs: 'h', showHelp() 't', teleport() So that I can have code something like: HashMap…
cwhiii
  • 1,496
  • 2
  • 14
  • 16
71
votes
8 answers

Why do I get a TypeError if I try to call a method directly from the class, supplying all arguments?

When I try this code: class MyStuff: def average(a, b, c): # Get the average of three numbers result = a + b + c result = result / 3 return result # Now use the function `average` from the `MyStuff`…
stu
  • 8,461
  • 18
  • 74
  • 112
69
votes
3 answers

inline function members inside a class

I know that declaring a function (normal function not a method inside a class) as inline is a good practice when the function definition is small for performance and it save time for the compilation. But how about inline methods inside a class I…
AlexDan
  • 3,203
  • 7
  • 30
  • 46