473,467 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

instance vs. static question

Can someone point me to a guide on when to use static methods versus
instance methods in a class? For instance, is it bad design to have a
static method that creates an instance of another class? I am looking for a
good explanation.

Thanks in advance
Nov 15 '05 #1
5 3617
"allison" wrote...
Can someone point me to a guide on when
to use static methods versus
instance methods in a class?
The simplest guide is to

- use instance methods when it's implementation
is depending on attributes, properties or
other instance methods for the instance itself.

- use static methods in every other case
For instance, is it bad design to have a
static method that creates an instance of
another class?


That depends...

If the method doesn't need any characteristics from the instance, that
method could very well be static.

On the other hand, there could possibly exist situations where a Singleton
pattern combined with some Factory Method pattern makes it preferable to
make it an instance method.

Hence, the answer to that question depends on what problem you're trying to
solve.

// Bjorn A
Nov 15 '05 #2
"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote:
The simplest guide is to

- use instance methods when it's implementation
is depending on attributes, properties or
other instance methods for the instance itself.


- or -

you want to make the method accessible by COM interop, which does not
support static methods.
Jens.

--
http://ManagedXLL.net/ | http://jens-thiel.de/ | http://QuantLib.net/
Replace MSDN with my first name when replying to my email address!
Nov 15 '05 #3
Allison...

http://www.geocities.com/jeff_louie/OOP/oop4.htm

Static fields are useful when you want to store state related to all
instances of a class. A counter is a good example of a static field. The
classic use of a static counter is to generate a unique ID or serial
number
for each instance of a class.*

Static methods are useful when you have behavior that is global to the
class and not specific to an instance of a class. In contrast, instance
methods are useful when the method needs to know about the state of
an object. Since data and behavior are intertwined in an object,
instance
methods have access to the instance fields and can exhibit behavior that
is specific to the state of an object.

A static method that creates an instance is an example of a class
factory.

Regards,
Jeff
Can someone point me to a guide on when to use static methods

versuinstance methods in a class? For instance, is it bad design to have
a
static method that creates an instance of another class<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4
"Jens Thiel" <MS**@Thiel.de> wrote in message
news:Od**************@TK2MSFTNGP09.phx.gbl...
"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote:
The simplest guide is to

- use instance methods when it's implementation
is depending on attributes, properties or
other instance methods for the instance itself.


- or -

you want to make the method accessible by COM interop, which does not
support static methods.
Jens.


That's a situation in itself. Good note though.

Jon
Nov 15 '05 #5

"allison" <nospam> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Can someone point me to a guide on when to use static methods versus
instance methods in a class? For instance, is it bad design to have a
static method that creates an instance of another class? I am looking for a good explanation.

Thanks in advance


Generally speaking, OOP purists tend to frown upon static methods unless
it's necessary, such as when there could not possibly be any way to have
instance state information or instance-specific methods to be called. On the
other hand, there are some cases when instantiating a class is a nuisance,
and making the methods non-static would make the task unnecessarily
non-trivial. The static methods of System.IO.File and System.IO.Directory
are good examples of this ... These could have been designed such that one
would have to create a Directory object (passing the path into a constructor
as a string) before you can get its files or access its other properties /
methods, but it made more sense from a design perspective -- that is, there
proved to be no otherwise advantage for developers, the users of the .NET
Framework -- to keep the methods static.

So, the long and short of it is, it really just depends. Ask yourself if
there could possibily be any need to instantiate an object. If not, don't
bother. Make it static. However, typically such instances should be far less
often than non-static, for the sake of OOP.

Jon
Nov 15 '05 #6

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

Similar topics

30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
1
by: Seb | last post by:
Is this efficient for a header file and a class? Does a 'static local' variable get created for each instance or include of the header file? Also, 'return _type().ToString();' does not seem to...
6
by: Dmitry Karneyev | last post by:
Hi! I guess this question have been asked a lot of times, but please be tolerant and if you have any ideas share it. The question is: how to make availibale only one instance of application and...
2
by: Mariano Drago | last post by:
Hi there. It will sound like a stupid question, but i cant figure out how to resolve this... Doing a little profiler job on the app im writing, i found a bottleneck in some parts that involve...
5
by: Diffident | last post by:
Hello All, I am designing a class based on singleton pattern. Inside this class I have multiple instance methods. My question is since there will be only one instance of this class at any...
3
by: lars.uffmann | last post by:
Hi everyone! I am debugging a big piece of code on the search for memory leaks, using g++ under suse 9.3. Since I'm trying to eliminate ALL memory leaks, I now stumbled upon a class foo that is...
5
by: Dinsdale | last post by:
I was discussing class architecture with one of the senior developers at my new job and him and I have a similar idea on how to work with data access and class libraries. That said, our...
13
by: JohnQ | last post by:
Why would anyone write: class SomeThing // class littered with non-domain single-instancing code :( { private: SomeThing(); static SomeThing* pInstance_; public: static SomeThing*...
3
by: Jon | last post by:
My main form opens up another form, and from this other form, I'd like to access things in the main form. The problem is that although I know the name of the class of the main form (FormMain) I...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.