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
81
votes
5 answers

How to determine by reflection if a Method returns 'void'

I have a java.lang.reflect.Method object and I would like to know if it's return type is void. I've checked the Javadocs and there is a getReturnType() method that returns a Class object. The thing is that they don't say what would be the return…
Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
81
votes
5 answers

Netbeans 7.4 introduces "10 lines max" per method rule. Where does this rule come from?

NetBeans 7.4 beta is currently available for public download, and it introduces a weird warning rule by default: Method length is 16 lines (10 allowed) My question is: Is this an accepted code convention rule, that can be proven somehow, somewhere…
Sliq
  • 15,937
  • 27
  • 110
  • 143
81
votes
8 answers

Can you get a method name from within a method in PHP?

Is it possible to do something like this? public function something() { $thisMethodName = method_get_name(); } Where method_get_name() returns the method's name?
alex
  • 479,566
  • 201
  • 878
  • 984
80
votes
12 answers

Why isn't calling a static method by way of an instance an error for the Java compiler?

I'm sure you all know the behaviour I mean - code such as: Thread thread = new Thread(); int activeCount = thread.activeCount(); provokes a compiler warning. Why isn't it an error? EDIT: To be clear: question has nothing to do with Threads. I…
tmtest
  • 1,541
  • 3
  • 15
  • 14
79
votes
5 answers

Utility class in Spring application - should I use static methods or not?

Let's say I have a utility class DateUtil (see below). To use this method a caller method uses DateUtils.getDateAsString(aDate). Would it be better to remove the static modifier and make DateUtil a spring bean (see DateUtilsBean) and inject it into…
user481572
78
votes
4 answers

Python 3 string.join() equivalent?

I've been using string.join() method in python 2 but it seems like it has been removed in python 3. What is the equivalent method in python 3? string.join() method let me combine multiple strings together with a string in between every other…
Dennis
  • 3,506
  • 5
  • 34
  • 42
78
votes
9 answers

Inheritance from multiple interfaces with the same method name

If we have a class that inherits from multiple interfaces, and the interfaces have methods with the same name, how can we implement these methods in my class? How can we specify which method of which interface is implemented?
masoud ramezani
  • 22,228
  • 29
  • 98
  • 151
78
votes
3 answers

Run Class methods in threads (python)

I'm currently learning Python and Classes and I have a basic question, but I didn't find any answer to it. Let's say I have this dummy class class DomainOperations: def __init__(self, domain): self.domain = domain self.domain_ip…
nacholibre
  • 3,874
  • 3
  • 32
  • 35
78
votes
3 answers

What does the registerNatives() method do?

In java, what does the private static method registerNatives() of the Object class do?
Hubris
  • 1,842
  • 2
  • 18
  • 26
77
votes
7 answers

Java Inheritance - calling superclass method

Lets suppose I have the following two classes public class alpha { public alpha(){ //some logic } public void alphaMethod1(){ //some logic } } public class beta extends alpha { public beta(){ //some…
roz
  • 791
  • 1
  • 5
  • 4
77
votes
12 answers

To use a read-only property or a method?

I need to expose the "is mapped?" state of an instance of a class. The outcome is determined by a basic check. It is not simply exposing the value of a field. I am unsure as to whether I should use a read-only property or a method. Read-only…
Dave New
  • 38,496
  • 59
  • 215
  • 394
76
votes
7 answers

Retrieving the calling method name from within a method

I have a method in an object that is called from a number of places within the object. Is there a quick and easy way to get the name of the method that called this popular method. Pseudo Code EXAMPLE: public Main() { PopularMethod(); } public…
user26901
76
votes
4 answers

undefined method `devise_for' in rails

After I install devise and create a user model. I rake db:migrate and then I rake routes. I then get a error with "undefined method `devise_for' for #". What could be causing this error?
Yeddie
  • 863
  • 2
  • 7
  • 7
76
votes
8 answers

Should I mark all methods virtual?

In Java you can mark method as final to make it impossible to override. In C# you have to mark method as virtual to make it possible to override. Does it mean that in C# you should mark all methods virtual (except a few ones that you don't want to…
Georgii Oleinikov
  • 3,865
  • 3
  • 27
  • 27
76
votes
6 answers

What is the difference between a function, an unbound method and a bound method?

I'm asking this question because of a discussion on the comment thread of this answer. I'm 90% of the way to getting my head round it. In [1]: class A(object): # class named 'A' ...: def f1(self): pass ...: In [2]: a = A() # an…
Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157