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
40
votes
4 answers

Java: Adding fields and methods to existing Class?

Is there, in Java, a way to add some fields and methods to an existing class? What I want is that I have a class imported to my code, and I need to add some fields, derived from the existing fields, and their returning methods. Is there any way to…
Pots Spi
  • 591
  • 1
  • 4
  • 4
40
votes
16 answers

Can there exist two main methods in a Java program?

Can two main methods exist in a Java program? Only by the difference in their arguments like: public static void main(String[] args) and second can be public static void main(StringSecond[] args) If it is possible, which Method will be used as the…
joey rohan
  • 3,505
  • 5
  • 33
  • 70
40
votes
8 answers

Method vs Property in C# - what's the difference

Possible Duplicate: Properties vs Methods In method you can type some code and in properties too. For example I have a property Name. When class name changes I would like to get some data from database and change state of my object. I can add…
Tom Smykowski
  • 25,487
  • 54
  • 159
  • 236
40
votes
6 answers

Method Chaining in Java

While answering a few questions on here earlier and from some work I have been doing lately I have been wondering why Java does not support method chaining on its built in classes. If I were to create a Car class for example, I could make it…
Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
39
votes
6 answers

how to call a method of multiple arguments with delay

I'm trying to call a method after some delay. I know there is a solution for that: [self performSelector:@selector(myMethod) withObject:nil afterDelay:delay]; I saw this question and Documentation But my question is: How can I call a method that…
Frade
  • 2,938
  • 4
  • 28
  • 39
39
votes
4 answers

Do class methods increase the size of the class instances?

The question is pretty straight forward. For clarity, consider the example below: // Note that none of the class have any data members // Or if they do have data members, they're of equal size, type, and quantity class Foo { public: void…
Zeenobit
  • 4,954
  • 3
  • 34
  • 46
39
votes
1 answer

Call methods of objects in array using array_map?

Suppose I have simple class like: class MyClass { private $_prop; public function getProp() {return $this->_prop;} [....] } Now what I want to do somwhere not in scope of MyClass is to get array of $_prop from array of objects of…
morphles
  • 2,424
  • 1
  • 24
  • 26
39
votes
4 answers

Eclipse organize methods in alphabetical order

I have a large class that contains about 30 methods. Is it possible to automatically sort them in alphabetical order in eclipse? I was hoping to do this so they would be easier to find when java browsing or looking at the class outline window.
39
votes
3 answers

C# Method Resolution, long vs int

class foo { public void bar(int i) { ... }; public void bar(long i) { ... }; } foo.bar(10); I would expect this code to give me some error, or at least an warning, but not so... What version of bar() is called, and why?
Vegar
  • 12,828
  • 16
  • 85
  • 151
39
votes
11 answers

What's the difference between `return;` and no return?

Is there a difference between: function someMethod( $someArg ) { // some code return; } and function someMethod( $someArg ) { // some code // no return } Both have NULL as 'return value'. Is there a difference? Something PHP internally?…
Rudie
  • 52,220
  • 42
  • 131
  • 173
39
votes
2 answers

Overriding method by another defined in module

I want to define an instance method Date#next which returns the next day. So I made a DateExtension module, like this: module DateExtension def next(symb=:day) dt = DateTime.now {:day => Date.new(dt.year, dt.month, dt.day + 1), …
resilva87
  • 3,325
  • 5
  • 32
  • 43
39
votes
6 answers

Calling a Activity method from BroadcastReceiver in Android

Here I am creating an online application that depends only on Internet. So whenever there is a network error it must notify user. For that, I have created a BroadcastReciver that receives call when network connection gets lost(Internet). All this…
38
votes
7 answers

Difference between "__method__" and "method"

What is the difference between __method__, method and _method__? Is there any or for some random reason people thought that __doc__ should be right like that instead of doc. What makes a method more special than the other?
Fabio
  • 1,113
  • 2
  • 14
  • 19
38
votes
6 answers

Why are getter and setter method important in java?

I have been taught to always use getters and setters. However, I don't know the pros and cons of these methods, as by implementing them we are exposing the data and also hiding it. I am a little confused about this. Can anybody give some proper…
School Boy
  • 1,207
  • 2
  • 19
  • 33
38
votes
5 answers

How to get list of controllers and actions in ruby on rails?

In rails, I can get the name of the current controller via controller_name and the current action by calling action_name. I am looking for similar runtime reflection for fetching the following: List of all controllers in the application. List of…
Tabrez
  • 3,424
  • 3
  • 27
  • 33