Difference Between a Static Method and a Non-Static Method

What is the difference between a static method and a non-static method?

A static method belongs to the class itself and a non-static (aka instance) method belongs to each object that is generated from that class. If your method does something that doesn't depend on the individual characteristics of its class, make it static (it will make the program's footprint smaller). Otherwise, it should be non-static.

Example:

class Foo {
int i;

public Foo(int i) {
this.i = i;
}

public static String method1() {
return "An example string that doesn't depend on i (an instance variable)";
}

public int method2() {
return this.i + 1; // Depends on i
}
}

You can call static methods like this: Foo.method1(). If you try that with method2, it will fail. But this will work: Foo bar = new Foo(1); bar.method2();

What is the difference between static methods in a Non static class and static methods in a static class?

The only difference is that static methods in a nonstatic class cannot be extension methods.


In other words, this is invalid:

class Test
{
static void getCount(this ICollection<int> collection)
{ return collection.Count; }
}

whereas this is valid:

static class Test
{
static void getCount(this ICollection<int> collection)
{ return collection.Count; }
}

Difference between static methods and nonstatic methods in object

float AverageWalkedKm(Person[] persons) can be static because it does not attempt to access any instance members (fields, properties, or methods) of the class.

void Walk (int Footsteps) cannot be static because it does access instance members of the class. (Namely, the Footsteps property.)

Note: instance members = non-static members.

EDIT

So, you object that in the AverageWalkedKm() the Person[] persons parameter is of datatype Person, so it seems to you that it is accessing instance members.

Well, yes, it is accessing instance members, but not of its own instance. It is given the instances that it works with. So, this method could belong to an entirely different class, and it would still work, unchanged, because it is given instances of Person to invoke. When we say that an instance method is capable of accessing instance members we mean of its own instance, as in this.member, or even without this., as it is optional.

Difference between Static methods and Instance methods

The basic paradigm in Java is that you write classes, and that those classes are instantiated. Instantiated objects (an instance of a class) have attributes associated with them (member variables) that affect their behavior; when the instance has its method executed it will refer to these variables.

However, all objects of a particular type might have behavior that is not dependent at all on member variables; these methods are best made static. By being static, no instance of the class is required to run the method.

You can do this to execute a static method:

MyClass.staticMethod();  // Simply refers to the class's static code

But to execute a non-static method, you must do this:

MyClass obj = new MyClass();  //Create an instance
obj.nonstaticMethod(); // Refer to the instance's class's code

On a deeper level the compiler, when it puts a class together, collects pointers to methods and attaches them to the class. When those methods are executed it follows the pointers and executes the code at the far end. If a class is instantiated, the created object contains a pointer to the "virtual method table", which points to the methods to be called for that particular class in the inheritance hierarchy. However, if the method is static, no "virtual method table" is needed: all calls to that method go to the exact same place in memory to execute the exact same code. For that reason, in high-performance systems it's better to use a static method if you are not reliant on instance variables.

Does static method in PHP have any difference with non-static method?

Except that, if you try to use $this in your method, like this :

class t {
protected $a = 10;
public function tt() {
echo $this->a;
echo 1;
}
}
t::tt();

You'll get a Fatal Error when calling the non-static method statically :

Fatal error: Using $this when not in object context in ...\temp.php on line 11

i.e. your example is a bit too simple, and doesn't really correspond to a real-case ;-)



Also note that your example should get you a strict warning (quoting) :

Calling non-static methods statically
generates an E_STRICT level warning.

And it actually does (At least, with PHP 5.3) :

Strict Standards: Non-static method t::tt() should not be called statically in ...\temp.php on line 12
1

So : not that good ;-)



Still, statically calling a non-static method doesnt't look like any kind of good practice (which is probably why it raises a Strict warning), as static methods don't have the same meaning than non-static ones : static methods do not reference any object, while non-static methods work on the instance of the class there're called on.



Once again : even if PHP allows you to do something (Maybe for historical reasons -- like compatibility with old versions), it doesn't mean you should do it !

Static and non static methods with the same name in parent class and implementing interface

A static and non static method can't have the same signature in the same class. This is because you can access both a static and non static method using a reference and the compiler will not be able to decide whether you mean to call the static method or the non static method.

Consider the following code for example :

Ov ov = new Ov();
ov.fun(); //compiler doesn't know whether to call the static or the non static fun method.

The reason why Java may allow a static method to be called using a reference is to allow developers to change a static method to a non static method seamlessly.



Related Topics



Leave a reply



Submit