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
49
votes
6 answers

Methods in Ruby: objects or not?

Inspired by this discussion, after some googling I wasn't able to find an answer to a pretty simple question regarding methods in Ruby: are methods objects or not? There are different opinions here and there, and I would really like to hear, let's…
Mladen Jablanović
  • 43,461
  • 10
  • 90
  • 113
49
votes
9 answers

Returning a "NULL reference" in C++?

In dynamically typed languages like JavaScript or PHP, I often do functions such as: function getSomething(name) { if (content_[name]) return content_[name]; return null; // doesn't exist } I return an object if it exists or null if…
laurent
  • 88,262
  • 77
  • 290
  • 428
48
votes
3 answers

jQuery call function from a string

Is it possible to call a function by using the strings ? (i.e) I have a variable var target = 'next';. Using this string I want to call the jquery method next() . Should i use target + '()' (this is bit foolish) to call next() ? I know it can be…
Aakash Chakravarthy
  • 10,523
  • 18
  • 61
  • 78
47
votes
7 answers

Difference between method calls $model->relation(); and $model->relation;

There is some basic understanding/theory here that I am missing.I don't understand the difference between these function calls: $distributors = $store->distributors(); $distributors = $store->distributors; $distributors =…
hodale
  • 717
  • 1
  • 6
  • 13
47
votes
2 answers

Very strange behavior of operator 'is' with methods

Why is the first result False, should it not be True? >>> from collections import OrderedDict >>> OrderedDict.__repr__ is OrderedDict.__repr__ False >>> dict.__repr__ is dict.__repr__ True
Chameleon
  • 9,722
  • 16
  • 65
  • 127
47
votes
5 answers

Why use 'reload' method after saving object? (Hartl Rails Tut 6.30)

I'm working on the exercises for chapter 6 of Hartl's Rails 4 Tutorial. The first exercise tests to make sure that user email addresses are down-cased correctly: require 'spec_helper' describe User do . . . describe "email address with…
sixty4bit
  • 7,422
  • 7
  • 33
  • 57
47
votes
18 answers

Android ViewPager setCurrentItem not working after onResume

I've got this strange issue, ViewPager's setCurrentItem(position, false) works perfectly fine, then im switching to another activity, and after I'm back to the first activity, the ViewPager always ends up on the first item. Even though I've added…
Maciej Boguta
  • 1,354
  • 1
  • 12
  • 15
46
votes
3 answers

C# - How to check if namespace, class or method exists in C#?

I have a C# program, how can I check at runtime if a namespace, class, or method exists? Also, how to instantiate a class by using it's name in the form of string? Pseudocode: string @namespace = "MyNameSpace"; string @class = "MyClass"; string…
kazinix
  • 28,987
  • 33
  • 107
  • 157
46
votes
4 answers

Can't Override onPostExecute() method in AsyncTask Class or get it to trigger

I am having trouble getting the onPostExecute() method to call when running an AsyncTask. When I try to set up my class extending AsyncTask in which the onPostExecute() is overridden I get the following build error. 'The method onPostExecute() of…
Ben
  • 515
  • 1
  • 6
  • 9
46
votes
3 answers

def block in rake task

I got undefined local variable or method 'address_geo' for main:Object with the following rake task. What's the problem with it? include Geokit::Geocoders namespace :geocode do desc "Geocode to get latitude, longitude and address" task :all =>…
Victor
  • 13,010
  • 18
  • 83
  • 146
46
votes
3 answers

c# - should I use "ref" to pass a collection (e.g. List) by reference to a method?

Should I use "ref" to pass a list variable by reference to a method? Is the answer that "ref" is not needed (as the list would be a reference variable), however for ease in readability put the "ref" in?
Greg
  • 34,042
  • 79
  • 253
  • 454
46
votes
4 answers

What's the difference between anonymous methods (C# 2.0) and lambda expressions (C# 3.0)?

What is the difference between anonymous methods of C# 2.0 and lambda expressions of C# 3.0.?
Codeslayer
  • 3,383
  • 7
  • 35
  • 42
46
votes
11 answers

In what situations is static method a good practice?

I have read the following discussions: Should private helper methods be static if they can be static , and Should all methods be static if their class has no member variables It seems that people in general would accept static methods, but are a…
Winston Chen
  • 6,799
  • 12
  • 52
  • 81
45
votes
3 answers

Python Class Based Decorator with parameters that can decorate a method or a function

I've seen many examples of Python decorators that are: function style decorators (wrapping a function) class style decorators (implementing __init__, __get__, and __call__) decorators which do not take arguments decorators which take…
Adam Parkin
  • 17,891
  • 17
  • 66
  • 87
45
votes
8 answers

Can I invoke a java method other than main(String[]) from the command line?

Can I invoke a java method other than main(String[]) from the command line?
Inversus
  • 3,125
  • 4
  • 32
  • 37