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

Why does Array.prototype.every return true on an empty array?

[].every(i => i instanceof Node) // -> true Why does the every method on arrays in JavaScript return true when the array is empty. I'm trying to do type assertion like so... const isT = (val, str) => typeof val === str const nT = (val, str) =>…
Saul does Code
  • 882
  • 2
  • 10
  • 13
52
votes
5 answers

Remove "Method is never used" warning for OnClick annotation in Android Studio

Sorry if this question has been asked before. I am using the Butterknife 5.0 with the latest version of Android Studio(0.5.7). How can I remove the "Method is never used" warning for methods that use the 'OnClick' Annotation of ButterKnife.I noticed…
TMS
  • 1,201
  • 1
  • 13
  • 20
52
votes
5 answers

C# dynamically set property

Possible Duplicate: .Net - Reflection set object property Setting a property by reflection with a string value I have an object with multiple properties. Let's call the object objName. I'm trying to create a method that simply updates the object…
David Archer
  • 2,008
  • 4
  • 26
  • 31
52
votes
4 answers

How does defining [square bracket] method in Ruby work?

I am going through Programming Ruby - a pragmatic programmers guide and have stumbled on this piece of code: class SongList def [](key) if key.kind_of?(Integer) return @songs[key] else for i in 0...@songs.length return…
oFca
  • 2,830
  • 6
  • 31
  • 43
51
votes
6 answers

Auto generate method comment in Xcode

Is there a way in Xcode to generate method comment automatically similar to what you do in Eclipse for javadoc comments. For example press you may hit /** one row before a method declaration and eclipse automatically generates the skeleton of…
L. Kvri
  • 1,456
  • 4
  • 23
  • 41
51
votes
9 answers

public methods in package-private classes

Does it make a difference to mark methods as public in package-private classes? class SomePackagePrivateClass { void foo(); // package private method public void bar(); // public method } Is there any practical difference in…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
51
votes
7 answers

What are the differences between functions and methods in Swift?

I always thought functions and methods were the same, until I was learning Swift through the "Swift Programming Language" eBook. I found out that I cannot use greet("John", "Tuesday") to call a function that I declared inside a class, as shown in…
Ricky
  • 10,485
  • 6
  • 36
  • 49
50
votes
5 answers

Calling a method from another method in the same class in C++

I wrote a method (that works fine) for a() in a class. I want to write another method in that class that calls the first method so: void A::a() { do_stuff; } void A::b() { a(); do_stuff; } I suppose I could just rewrite b() so b(A obj) but…
devin
  • 6,407
  • 14
  • 48
  • 53
50
votes
4 answers

If a synchronized method calls another non-synchronized method, is there a lock on the non-synchronized method

In Java, if a synchronized method contains a call to a non-synchronized, can another method still access the non-synchronized method at the same time? Basically what I'm asking is everything in the synchronized method have a lock on it (including…
dido
  • 3,347
  • 11
  • 34
  • 42
50
votes
9 answers

In Java, what is the difference between this.method() and method()?

Is there any difference between calling this.method() and method() (including performance difference)?
nebkat
  • 8,445
  • 9
  • 41
  • 60
50
votes
5 answers

Extension methods conflict

Lets say I have 2 extension methods to string, in 2 different namespaces: namespace test1 { public static class MyExtensions { public static int TestMethod(this String str) { return 1; } }…
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
50
votes
8 answers

The method clone() from object is not visible?

Question: package GoodQuestions; public class MyClass { MyClass() throws CloneNotSupportedException { try { throw new CloneNotSupportedException(); } catch(Exception e) { e.printStackTrace(); } …
sekhar
  • 710
  • 1
  • 7
  • 13
49
votes
16 answers

Set and Get Methods in java?

How can I use the set and get methods, and why should I use them? Are they really helpful? And also can you give me examples of set and get methods?
user759630
  • 679
  • 2
  • 8
  • 11
49
votes
4 answers

"call to undefined function" error when calling class method

this is the error Fatal error: Call to undefined function assign( this is the code, as you can see i obviously have defined the function so why is it not working class shades { function create($name, $shades, $slug, $shortDesc, $longDesc,…
Tommy Arnold
  • 3,339
  • 8
  • 31
  • 40
49
votes
3 answers

JavaScript Array#map: index argument

My question is about the map method of arrays in JavaScript. You can pass it a function that takes a second argument, the index of the current element of the array being processed, but... to what purpose? What happens when you do it and what's the…
Claudio
  • 593
  • 1
  • 4
  • 5