Nona Urbiz
Nona Urbiz

Reputation: 5003

Why use an object instance rather than class::staticFunction?

Why should I use an object instance to access member functions rather than class::staticFunction?

( or why not? )

Upvotes: 0

Views: 210

Answers (4)

codymanix
codymanix

Reputation: 29468

  • instance methods belong to a single instance/object to which state/variables they have access.

  • static methods belong to a whole class and no specific instance/object and do not have access to any instance members. They only can use other static members.

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490108

You're allowed to use the object.function() notation for a static function, but I'd advise against it -- it gives the misleading impression that the function is associated with the specific object, like with a non-static member function. Using the classname::function() syntax portrays the situation clearly and accurately.

Upvotes: 3

Alex Brown
Alex Brown

Reputation: 42872

Because the object contains the variables that the method might act on.

If you don't use this facility, you are not using OOP (Object Oriented Programming), you are using perl modules.

On the other hand, sometimes what you propose–just using static functions–is appropriate.

Upvotes: 1

rlbond
rlbond

Reputation: 67749

Static members of a class only hold global state. Instances each have their own state.

Upvotes: 0

Related Questions