473,900 Members | 3,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
13 10747
> 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.create AllShoppingTrol leys()

Cons:
1. Poor naming conventions can make it hard to know that is going on
eg Whatever.getGiz mo() 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.create AllShoppingTrol leys()

Cons:
1. Poor naming conventions can make it hard to know that is going on
eg Whatever.getGiz mo() 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.create AllShoppingTrol leys()

Cons:
1. Poor naming conventions can make it hard to know that is going on
eg Whatever.getGiz mo() 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.2898 8$af3.1594862@a ttbi_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
2394
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 'unverified' subscribers in a different table, aside from organization? I did just that, where, when they subscribe, they are put in a temp table, and them moved to the real table once verified (and the temp entry deleted). At the time, I did this...
0
1854
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. Besides being able to use more RAM and having to use Service pack 3a in Win 2003 Any pros/cons of both ? Any recommendations ?
3
2088
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 copy of data as opposed to multiple instances of the class) C# is now applying the static concept to methods. Why?, I thought that only one copy of the methods were loaded into memory not matter how many instances were created. Is this different...
4
2057
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 changing because they are not thread-safe)." Can someone please elaborate on the pros and cons of using static methods? (In VB they are called Shared methods.) The MS DAAB uses Shared methods. This means that all Data Access calls go through...
5
2330
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 are stability and security. What are the pros/cons/issues for PHP4/PHP5 with apache (either 1.3 or 2)? TIA,
12
3587
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
1579
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
2947
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? Python is a powerful, easy-to-use scripting language suitable for use in the enterprise, although it is not right for absolutely every use. Python expert Martin Aspeli identifies when Python is the right choice, and when another language might be a...
3
1874
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
9997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9845
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8043
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6082
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4301
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3320
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.