473,326 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Pros and Cons of Static Methods

Okay, I'm in a debate over whether or not static methods are a good idea in
a general domain class.

I'm personally not a fan of static methods but we seem to be using them to
load an object. For example if you have an Employee class rather then
instantiating an instance you call a static method 'GetEmployees' and it
returns a List of Employee objects.

I'm looking for what other people are doing and if you feel this is a good
or bad idea.

Thanks
Jul 17 '05 #1
13 10646
things are done differently in Java than C++, you are in more of truely
object-oriented world and thats not just a cute term. static methods are
really class messages, that is, methods that act upon an entire
classification of objects not just one instance. it is an elegant means
by which to send messages to all instances or rather the class itself
and thats how you have to start thinking.

i know your sort of coming at it from an anti-global variable approach
but static methods are not the same thing. typically they do not create
the same hang ups as global methods used in non object-oriented languages.

i could say more but first let me hear back if you understand what i am
saying

thanks

- perry
Axehelm wrote:
Okay, I'm in a debate over whether or not static methods are a good idea in
a general domain class.

I'm personally not a fan of static methods but we seem to be using them to
load an object. For example if you have an Employee class rather then
instantiating an instance you call a static method 'GetEmployees' and it
returns a List of Employee objects.

I'm looking for what other people are doing and if you feel this is a good
or bad idea.

Thanks


Jul 17 '05 #2
Yes I understand what you are saying and yes I'm ant-global... :)

It just goes against what I've been taught about OO. For example a static
method cannot be overridden... correct? That seems to go against
polymorphism. Exposing multiple static methods in place of constructors
seems a bit unOO if you will to me.

"perry anderson" <pe***@cplusplus.org> wrote in message
news:xz********************@news20.bellglobal.com. ..
things are done differently in Java than C++, you are in more of truely
object-oriented world and thats not just a cute term. static methods are
really class messages, that is, methods that act upon an entire
classification of objects not just one instance. it is an elegant means
by which to send messages to all instances or rather the class itself
and thats how you have to start thinking.

i know your sort of coming at it from an anti-global variable approach
but static methods are not the same thing. typically they do not create
the same hang ups as global methods used in non object-oriented languages.

i could say more but first let me hear back if you understand what i am
saying

thanks

- perry
Axehelm wrote:
Okay, I'm in a debate over whether or not static methods are a good idea in a general domain class.

I'm personally not a fan of static methods but we seem to be using them to load an object. For example if you have an Employee class rather then
instantiating an instance you call a static method 'GetEmployees' and it
returns a List of Employee objects.

I'm looking for what other people are doing and if you feel this is a good or bad idea.

Thanks

Jul 17 '05 #3

"Axehelm" <ax*****@mchsi.com> wrote in message
news:F3atc.11571$eT4.650@attbi_s54...
Yes I understand what you are saying and yes I'm ant-global... :)

It just goes against what I've been taught about OO. For example a static
method cannot be overridden... correct? That seems to go against
polymorphism. Exposing multiple static methods in place of constructors
seems a bit unOO if you will to me.

"perry anderson" <pe***@cplusplus.org> wrote in message
news:xz********************@news20.bellglobal.com. ..
things are done differently in Java than C++, you are in more of truely
object-oriented world and thats not just a cute term. static methods are
really class messages, that is, methods that act upon an entire
classification of objects not just one instance. it is an elegant means
by which to send messages to all instances or rather the class itself
and thats how you have to start thinking.

i know your sort of coming at it from an anti-global variable approach
but static methods are not the same thing. typically they do not create
the same hang ups as global methods used in non object-oriented languages.

i could say more but first let me hear back if you understand what i am
saying

thanks

- perry
Axehelm wrote:
Okay, I'm in a debate over whether or not static methods are a good idea
in a general domain class.

I'm personally not a fan of static methods but we seem to be using
them
to load an object. For example if you have an Employee class rather then
instantiating an instance you call a static method 'GetEmployees' and
it returns a List of Employee objects.

I'm looking for what other people are doing and if you feel this is a

good or bad idea.

Thanks



You are completely right. Static methods are what the OO-purists came up
with when they discovered that there are actually good reasons for having
non-class-member functions a-la C++ (which BTW also has static methods). The
static void main stuff is the most prominent example of that.

Static methods are usefull because they give you an escape of the
everything-is-or-should-be-an-object concept. There is nothing wrong with
using them as long as you know how to.

Silvio Bierman
Jul 17 '05 #4
> It just goes against what I've been taught about OO. For example a static
method cannot be overridden... correct? That seems to go against
polymorphism. Exposing multiple static methods in place of constructors
seems a bit unOO if you will to me.


You've picked on a classic issue that I personally have never found an
answer to one way or the other.

As an example - which of these is best (using your example):

class Employee
{
...
public static Collection getAllEmployees() { ... }
}

Collection allEmployees = Employee.getAllEmployees();

or:

class Employee { ... }

class EmployeeMaintenance // or some such name
{
public Collection getAllEmployees() { ... }
}

EmployeeMaintenance em = new EmployeeMaintenance();
Collection allEmployees = em.getAllEmployees();

Hard to say really IMHO.

If there was just one or two static methods on a class then I'd say
the first because it's easier to maintain. If the 'maintenance' class
expanded to many methods (searches, CRUD stuff, etc) then a separate
class is probably better for cohesion reasons.

As to whether static methods are not OO or not, I don't know, it's
probably only of interest to academics.

- sarge
Jul 17 '05 #5
"Chris" <sa*********@hotmail.com> wrote in message
news:56**************************@posting.google.c om...
It just goes against what I've been taught about OO. For example a static method cannot be overridden... correct? That seems to go against
polymorphism. Exposing multiple static methods in place of constructors
seems a bit unOO if you will to me.
You've picked on a classic issue that I personally have never found an
answer to one way or the other.

As an example - which of these is best (using your example):

class Employee
{
...
public static Collection getAllEmployees() { ... }
}

Collection allEmployees = Employee.getAllEmployees();

Redundant naming. Try Employee.getAll().
or:

class Employee { ... }

class EmployeeMaintenance // or some such name
{
public Collection getAllEmployees() { ... }
}

EmployeeMaintenance em = new EmployeeMaintenance();
Collection allEmployees = em.getAllEmployees();
What's an "employee maintenance"? OOD is about modelling real world objects.
It sounds like you're still thinking procedurally.
Hard to say really IMHO.
Not hard for me. In the latter example, how many of the "employee
maintenance" objects are necessary? You should only ever need one, correct?
Allowing more than one to be instantiated is inefficient. So you have a
couple of options: make the methods static and give it a private constructor
or implement the Singleton pattern, which also involves a static method.
Whichever way you go, static methods are your best option.
If there was just one or two static methods on a class then I'd say
the first because it's easier to maintain. If the 'maintenance' class
expanded to many methods (searches, CRUD stuff, etc) then a separate
class is probably better for cohesion reasons.

Cohesion? That's no OOD term I've heard of.
Jul 17 '05 #6
Classes were not meant to become repositories for functions.
Using static methods. What you in fact have, is a function.

perry anderson wrote:
things are done differently in Java than C++, you are in more of truely
object-oriented world and thats not just a cute term. static methods are
really class messages, that is, methods that act upon an entire
classification of objects not just one instance. it is an elegant means
by which to send messages to all instances or rather the class itself
and thats how you have to start thinking.

i know your sort of coming at it from an anti-global variable approach
but static methods are not the same thing. typically they do not create
the same hang ups as global methods used in non object-oriented languages.

i could say more but first let me hear back if you understand what i am
saying

thanks

- perry
Axehelm wrote:
Okay, I'm in a debate over whether or not static methods are a good
idea in
a general domain class.

I'm personally not a fan of static methods but we seem to be using
them to
load an object. For example if you have an Employee class rather then
instantiating an instance you call a static method 'GetEmployees' and it
returns a List of Employee objects.

I'm looking for what other people are doing and if you feel this is a
good
or bad idea.

Thanks


Jul 17 '05 #7
Silvio Bierman wrote:


You are completely right. Static methods are what the OO-purists came up
with when they discovered that there are actually good reasons for having
non-class-member functions a-la C++ (which BTW also has static methods). The
static void main stuff is the most prominent example of that.

Static methods are usefull because they give you an escape of the
everything-is-or-should-be-an-object concept. There is nothing wrong with
using them as long as you know how to.

Class methods doesn't need to be an escape from the pure oo concept.
Look at for instance Smalltalk where you have class methods (a kind of
static). The classes themselves are instances of meta classes therefore
the "static" methods are just ordinary methods with the full inheritance
scheme (with abilities to override and call superclass's version of the
class method).
Smalltalk does not have an "operator" new, and therefore there isn't any
need for constructors either, so to instantiate a Smalltalk object one
has to send a message to the intended class. Very often one uses the
message new; like in Set new, where the message new is sent to the class
Set; but it is not uncommon to use other class methods to instantiate
like in Circle center: 10@10 radius: 45.

By using class methods to create new instances one directly exploit the
well know Abstract Factory pattern.
E.g. Filename named: 'test.text' gives and instance of NTFSFilename on a
Windows XP platform and MacOSXFilename on a Mac OS 10 platform, etc.
That is it is possible to hide platform specific details and
instantiations from the application programmer. And thereby, among other
things, one get more portable code.
Björn
Jul 17 '05 #8
I hadn't meant the code examples to be examples of good practice, just
illustrating the point.

I'm surprised you've never heard the term 'cohesion', as most of the
books and articles I read are riddled with it - maybe I'm reading the
wrong stuff.

- sarge
Jul 17 '05 #9
> I'm personally not a fan of static methods but we seem to be using them to
load an object. For example if you have an Employee class rather then
instantiating an instance you call a static method 'GetEmployees' and it
returns a List of Employee objects.

I'm looking for what other people are doing and if you feel this is a good
or bad idea.


Hi,

IN A NUTSHELL
I know what you mean but the scenario your describe is pretty normal and ok.

That type of constructor is called a "static factory", in some circumstances
they are actually preferable. Sun use them quite a bit in Java itself.
Things like Xyz.getInstance(); Of course like everything else they need to
be used wisely.

MORE DETAILS
Pros of construction using static factory methods:
Generally you get more control and flexibility.

1. Regular constructors will create an object every time they are invoked
and only when they are invoked, but with a static factory you can have
pre-built objects. Sometimes this flexibility of *when* things are really
built is highly desirable.

2. Or you can defer real construction until the object is actually used
a.k.a lazy instantiation.
static private Blah blah;
static public Blah createBlah() {
if(blah==null)
blah= new Blah();
return blah;
}
This is good for "expensive" objects, they only get created if/when they
are used.

3. Can return any type.
eg return Collection of Whatever's
Your GetEmployees() is a good example.

4. static factory's can have meaningful names, this can be good
Whatever.createAllShoppingTrolleys()

Cons:
1. Poor naming conventions can make it hard to know that is going on
eg Whatever.getGizmo() could be a constructor but with that kind of name
you'd never know.

2. You cannot subclass (or extend) a class which only has static factory
constructors. You must use containment rather than inheritance.
Hope that helps?
Alex K

Jul 17 '05 #10
> I'm personally not a fan of static methods but we seem to be using them to
load an object. For example if you have an Employee class rather then
instantiating an instance you call a static method 'GetEmployees' and it
returns a List of Employee objects.

I'm looking for what other people are doing and if you feel this is a good
or bad idea.


Hi,

IN A NUTSHELL
I know what you mean but the scenario your describe is pretty normal and ok.

That type of constructor is called a "static factory", in some circumstances
they are actually preferable. Sun use them quite a bit in Java itself.
Things like Xyz.getInstance(); Of course like everything else they need to
be used wisely.

MORE DETAILS
Pros of construction using static factory methods:
Generally you get more control and flexibility.

1. Regular constructors will create an object every time they are invoked
and only when they are invoked, but with a static factory you can have
pre-built objects. Sometimes this flexibility of *when* things are really
built is highly desirable.

2. Or you can defer real construction until the object is actually used
a.k.a lazy instantiation.
static private Blah blah;
static public Blah createBlah() {
if(blah==null)
blah= new Blah();
return blah;
}
This is good for "expensive" objects, they only get created if/when they
are used.

3. Can return any type.
eg return Collection of Whatever's
Your GetEmployees() is a good example.

4. static factory's can have meaningful names, this can be good
Whatever.createAllShoppingTrolleys()

Cons:
1. Poor naming conventions can make it hard to know that is going on
eg Whatever.getGizmo() could be a constructor but with that kind of name
you'd never know.

2. You cannot subclass (or extend) a class which only has static factory
constructors. You must use containment rather than inheritance.
Hope that helps?
Alex K

Jul 17 '05 #11
> I'm personally not a fan of static methods but we seem to be using them to
load an object. For example if you have an Employee class rather then
instantiating an instance you call a static method 'GetEmployees' and it
returns a List of Employee objects.

I'm looking for what other people are doing and if you feel this is a good
or bad idea.


Hi,

IN A NUTSHELL
I know what you mean but the scenario your describe is pretty normal and ok.

That type of constructor is called a "static factory", in some circumstances
they are actually preferable. Sun use them quite a bit in Java itself.
Things like Xyz.getInstance(); Of course like everything else they need to
be used wisely.

MORE DETAILS
Pros of construction using static factory methods:
Generally you get more control and flexibility.

1. Regular constructors will create an object every time they are invoked
and only when they are invoked, but with a static factory you can have
pre-built objects. Sometimes this flexibility of *when* things are really
built is highly desirable.

2. Or you can defer real construction until the object is actually used
a.k.a lazy instantiation.
static private Blah blah;
static public Blah createBlah() {
if(blah==null)
blah= new Blah();
return blah;
}
This is good for "expensive" objects, they only get created if/when they
are used.

3. Can return any type.
eg return Collection of Whatever's
Your GetEmployees() is a good example.

4. static factory's can have meaningful names, this can be good
Whatever.createAllShoppingTrolleys()

Cons:
1. Poor naming conventions can make it hard to know that is going on
eg Whatever.getGizmo() could be a constructor but with that kind of name
you'd never know.

2. You cannot subclass (or extend) a class which only has static factory
constructors. You must use containment rather than inheritance.
Hope that helps?
Alex K

Jul 17 '05 #12
> I'm personally not a fan of static methods but we seem to be using them to
load an object. For example if you have an Employee class rather then
instantiating an instance you call a static method 'GetEmployees' and it
returns a List of Employee objects.

I'm looking for what other people are doing and if you feel this is a good
or bad idea.


Hi,

IN A NUTSHELL
I know what you mean but the scenario your describe is pretty normal and ok.

That type of constructor is called a "static factory", in some circumstances
they are actually preferable. Sun use them quite a bit in Java itself.
Things like Xyz.getInstance(); Of course like everything else they need to
be used wisely.

MORE DETAILS
Pros of construction using static factory methods:
Generally you get more control and flexibility.

1. Regular constructors will create an object every time they are invoked
and only when they are invoked, but with a static factory you can have
pre-built objects. Sometimes this flexibility of *when* things are really
built is highly desirable.

2. Or you can defer real construction until the object is actually used
a.k.a lazy instantiation.
static private Blah blah;
static public Blah createBlah() {
if(blah==null)
blah= new Blah();
return blah;
}
This is good for "expensive" objects, they only get created if/when they
are used.

3. Can return any type.
eg return Collection of Whatever's
Your GetEmployees() is a good example.

4. static factory's can have meaningful names, this can be good
Whatever.createAllShoppingTrolleys()

Cons:
1. Poor naming conventions can make it hard to know that is going on
eg Whatever.getGizmo() could be a constructor but with that kind of name
you'd never know.

2. You cannot subclass (or extend) a class which only has static factory
constructors. You must use containment rather than inheritance.
Hope that helps?
Alex K

Jul 17 '05 #13
Use each when warranted.
For instance, in a class 'Person';;; fields like name, address, height,
weight should be instance vars, but it would make sense for personCount (the
number of "Person' objects instantiated) to be static - it belongs to the
class along with the set/get methods for it.
"Axehelm" <ax*****@mchsi.com> wrote in message
news:KI8tc.28988$af3.1594862@attbi_s51...
Okay, I'm in a debate over whether or not static methods are a good idea in a general domain class.

I'm personally not a fan of static methods but we seem to be using them to
load an object. For example if you have an Employee class rather then
instantiating an instance you call a static method 'GetEmployees' and it
returns a List of Employee objects.

I'm looking for what other people are doing and if you feel this is a good
or bad idea.

Thanks

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 5/22/2004
Jul 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sniffle | last post by:
Thanks... Say you have a double opt in mailing list, of which the subcriber list is store in the db. Im still somewhat of a newb, so bear with me... are there any pros/cons as to keeping the...
0
by: Steve | last post by:
We've recently got a new Server in. The server has 16Gb of RAM, 8 cpus etc We now have a choice of sticking with Windows 2000 Advanced Server or going with Windows 2003 Enterprise edition. ...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
4
by: Joe Fallon | last post by:
In another post Kevin Spencer stated: "one should be careful of using static fields, properties, and methods, by understanding what the implications of such are (e.g. locking static variables when...
5
by: sinister | last post by:
I'm starting a database/web interface project, using Linux and postgresql. I've programmed in PHP4 in the past, and for this new project am unsure whether to use PHP4 or PHP5. My main concerns...
12
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
1
by: Doogie | last post by:
I'm looking for some good reading material on the pros/cons of using the Session object and was wondering if anyone could point me in the right direction?
62
by: estherschindler | last post by:
This is part of a series examining the strengths and weaknesses of various scripting languages, with particular attention to enterprise (read: big company) use. You Used Python to Write WHAT?...
3
by: ziycon | last post by:
What would be the pros/cons of turning off error warnings in the website pages like below? What are peoples opinions on this approach? <?php error_reporting(0); ?>
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.