tce,
I use static methods and members fairly infrequently -- and most of those
are general purpose helper or utility functions that require no instance
data other than whatever is passed in via arguments.
It may help to think of it this way: a class represents a a real-world thing
or concept (or an abstraction of one) including its data (fields /
properties) and behaviors (methods). It's just a way to organize data and
operations on that data into a coherent package. In general, most "things"
a program deals with have data that varies from one "instance" to another.
You will create objects from these classes. If you have methods that are
appropriate for more than one class then you may have organized the code
incorrectly -- or you may actually have a general purpose utility function
that can be a static method of a utility class that may never be
instantiated.
If you have some data that would be the same for *any* instance of a class,
then make it a static field of that class. If you have some action that
would be the same for *any* instance of a class, make it a static method of
that class. Otherwise, yes, create objects, and don't worry about having a
lot of them -- quantity is not so much the issue as coherent organization.
Remember that the object code associated with an object is only loaded once
no matter how many instances you have. Only the instance data requires
per-instance memory storage. Most objects don't have that much instance
data, so you really aren't going to eat up all your RAM in a hurry.
Assuming you use basic common sense in your designs up front, worry about
having too many object instances only if it becomes a real-world resource or
performance issue.
--Bob
"thechaosengine" <sh856531@microsofts_free_email_service.com> wrote in
message news:en3VLXU4EHA.3336@TK2MSFTNGP11.phx.gbl...
[quoted text, click to view] > Hi all,
>
> I have a very general but quite significant question about objects.
>
> My question is, when should I create them? I know thats a crap question so
> let me explain a bit further.
>
> Lets take an example of user management against a database. Things I might
> like to do include:
>
> - Creating a new user
> - Changing a users phone number
> - Deleting a user
> - Getting a users age
> - Updating a users password
>
> The thing with all this is not one of those operations require the
> instantiation of a User object. You could do it through static methods.
>
> The thing is, you the sort of operations above must make up 80% or so of
> all operations on users. Likewise, operations similar to these ones effect
> most business entities, be it a Bug or a Role or a Car object.
>
> So my question is - is it really the case, that a good proportion of the
> time, you dont need to instantiate an object at all to achieve your aims,
> or am I using a poor approach to design my applications?
>
> Many thanks to anyone who can share some advice.
>
> Kindest Regards
>
> tce