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

Python-like list comprehension in Java

Since Java doesn't allow passing methods as parameters, what trick do you use to implement Python like list comprehension in Java ? I have a list (ArrayList) of Strings. I need to transform each element by using a function so that I get another…
euphoria83
  • 14,768
  • 17
  • 63
  • 73
69
votes
2 answers

Varargs Java Ambiguous Call

I'm a little confused about Java's varargs methods: public static int sum(int ...a) { return 0; } public static double sum(double ...a) { return 0.0; } When I tried to invoke sum() without passing any argument, then the int version of…
KarimS
  • 3,812
  • 9
  • 41
  • 64
69
votes
3 answers

to_s vs. to_str (and to_i/to_a/to_h vs. to_int/to_ary/to_hash) in Ruby

I'm learning Ruby and I've seen a couple of methods that are confusing me a bit, particularly to_s vs to_str (and similarly, to_i/to_int, to_a/to_ary, & to_h/to_hash). What I've read explains that the shorter form (e.g. to_s) are for explicit…
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
68
votes
5 answers

The reason to use JS .call() method?

I'm interested what's the reason to have call() method in JS. It seems it duplicates usual method of calling this. For example, I have a code with call(). var obj = { objType: "Dog" } f = function(did_what, what) { alert(this.objType + " "…
Green
  • 28,742
  • 61
  • 158
  • 247
68
votes
7 answers

C#: Function in Function possible?

Is it possible to declare a method within another method in C#? For example like that: void OuterMethod() { int anything = 1; InnerMethod(); // call function void InnerMethod() { int PlitschPlatsch = 2; } }
Non
  • 1,936
  • 2
  • 17
  • 24
68
votes
4 answers

Why do I get a TypeError that says "takes no arguments (1 given)"?

I have this code to implement a Particle Swarm Optimization algorithm: class Particle: def __init__(self,domain,ID): self.ID = ID self.gbest = None self.velocity = [] self.current = [] self.pbest = [] …
Linus
  • 1,113
  • 1
  • 8
  • 14
68
votes
2 answers

What does the Ruby method 'to_sym' do?

What does the to_sym method do? What is it used for?
keruilin
  • 16,782
  • 34
  • 108
  • 175
67
votes
3 answers

mock nested method calls using mockito

I have got 4 classes lets says A, B, C, D each calling on methods from another one. now I have mocked class A, and want to mock a method using mockito A a = Mockito.mock(A.class); and want to get "foo" on recursive method calls like…
Abhijeet Ahuja
  • 5,596
  • 5
  • 42
  • 50
67
votes
4 answers

How can I get the value of a session variable inside a static method?

I am using ASP.NET page methods with jQuery.... How do I get the value of a session variable inside a static method in C#? protected void Page_Load(object sender, EventArgs e) { Session["UserName"] = "Pandiya"; } [WebMethod] public static…
ACP
  • 34,682
  • 100
  • 231
  • 371
67
votes
6 answers

Why does a function call require the parameter name in Swift?

I have this Function in a class: func multiply(factor1:Int, factor2:Int) -> Int{ return factor1 * factor2 } I try to call the function using this: var multResult = calculator.multiply(9834, 2321) The problem is that the compiler wants it to…
67cherries
  • 6,931
  • 7
  • 35
  • 51
67
votes
9 answers

Method to get all files within folder and subfolders that will return a list

I have a method that will iterate through a folder and all of its subfolders and get a list of the file paths. However, I could only figure out how to create it and add the files to a public List, but not how to return the list. Here's the…
Wilson
  • 8,570
  • 20
  • 66
  • 101
66
votes
4 answers

Why is Main method private?

New console project template creates a Main method like this: class Program { static void Main(string[] args) { } } Why is it that neither the Main method nor the Program class need to be public?
František Žiačik
  • 7,511
  • 1
  • 34
  • 59
65
votes
2 answers

functools.partial on class method

I'm trying to define some class methods using another more generic class method as follows: class RGB(object): def __init__(self, red, blue, green): super(RGB, self).__init__() self._red = red self._blue = blue …
Arjor
  • 979
  • 1
  • 8
  • 12
64
votes
4 answers

Nine ways to define a method in Scala?

So I've been trying to puzzle through the various ways you can define stuff in Scala, complicated by my lack of understanding of the way {} blocks are treated: object NewMain extends Thing{ def f1 = 10 def f2 {10} def f3 = {10} def…
Li Haoyi
  • 15,330
  • 17
  • 80
  • 137
63
votes
3 answers

Python calling method in class

I'm punching way above my weight here, but please bear with this Python amateur. I'm a PHP developer by trade and I've hardly touched this language before. What I'm trying to do is call a method in a class...sounds simple enough? I'm utterly baffled…
kirgy
  • 1,567
  • 6
  • 23
  • 39