Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between class and instance methods?

What's the difference between a class method and an instance method?

Are instance methods the accessors (getters and setters) while class methods are pretty much everything else?

like image 424
Devoted Avatar asked Jun 27 '09 20:06

Devoted


People also ask

What is the difference between a class method and an instance method java?

Instance methods can access class variables and class methods directly. Class methods can access class variables and class methods directly. Class methods cannot access instance variables or instance methods directly—they must use an object reference.

What is the difference between static class method and instance method?

Instance method are methods which require an object of its class to be created before it can be called. Static methods are the methods in Java that can be called without creating an object of class.

What is difference between instance method and class method in Ruby?

In Ruby, a method provides functionality to an Object. A class method provides functionality to a class itself, while an instance method provides functionality to one instance of a class.

What is the instance methods?

An instance method is a method that belongs to instances of a class, not to the class itself. To define an instance method, just omit static from the method heading. Within the method definition, you refer to variables and methods in the class by their names, without a dot.


2 Answers

Like most of the other answers have said, instance methods use an instance of a class, whereas a class method can be used with just the class name. In Objective-C they are defined thusly:

@interface MyClass : NSObject  + (void)aClassMethod; - (void)anInstanceMethod;  @end 

They could then be used like so:

[MyClass aClassMethod];  MyClass *object = [[MyClass alloc] init]; [object anInstanceMethod]; 

Some real world examples of class methods are the convenience methods on many Foundation classes like NSString's +stringWithFormat: or NSArray's +arrayWithArray:. An instance method would be NSArray's -count method.

like image 128
Cory Kilger Avatar answered Oct 17 '22 12:10

Cory Kilger


All the technical details have been nicely covered in the other answers. I just want to share a simple analogy that I think nicely illustrates the difference between a class and an instance:

enter image description here

A class is like the blueprint of a house: You only have one blueprint and (usually) you can't do that much with the blueprint alone.

enter image description here

An instance (or an object) is the actual house that you build based on the blueprint: You can build lots of houses from the same blueprint. You can then paint the walls a different color in each of the houses, just as you can independently change the properties of each instance of a class without affecting the other instances.

like image 36
Johannes Fahrenkrug Avatar answered Oct 17 '22 13:10

Johannes Fahrenkrug