Patterns & Practices : Static Methods

Get help on programming - C++, Java, Delphi, etc.
Post Reply
GrimStoner
Registered User
Posts: 716
Joined: 08 Oct 2004, 02:00
Contact:

Patterns & Practices : Static Methods

Post by GrimStoner »

I've just installed DevXpress CodeRush/Refactor Pro, which analyzes your code as you go along, and gives suggestions as to improving it.

In one of my existing projects, one that pops up quite often is "Member can be static". Why is it advisable to make all my members static?
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Re: Patterns & Practices : Static Methods

Post by rustypup »

it most certainly isn't... static instances are really light on RAM, but are commonly open to abuse and are a healthy environment for growing bugs...
Most people would sooner die than think; in fact, they do so - Bertrand Russel
-Prometheus-
Resident Drama Llama
Posts: 967
Joined: 05 Mar 2008, 02:00
Contact:

Re: Patterns & Practices : Static Methods

Post by -Prometheus- »

........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
Last edited by -Prometheus- on 03 Apr 2011, 22:35, edited 1 time in total.
BBLounge - Broadband and Technology forum
Please like our facebook page
User avatar
Ron2K
Forum Technical Administrator
Posts: 9050
Joined: 04 Jul 2006, 16:45
Location: Upper Hutt, New Zealand
Contact:

Re: Patterns & Practices : Static Methods

Post by Ron2K »

-Prometheus- wrote:Don't know what language you are using but it certainly should NOT be static unless you know the reason it should be static. Since you're asking this I'm assuming you don't. As you are referring to members I'm also assuming you are talking about classes.

Static member variables exist as instances of a class and not instances of the objects themselves. That is when you declare the class it makes one variable for that class no matter how many objects or if you even have an object of the class. Think of them as global variables that are accessible only by the class and in terms of the class. They are actually global variables with different access rights and are usually used to keep track of how many objects you create and destroy.

Member functions or methods can access static members. Static methods can be accessed without making an instance of the class. Even with non-static methods there's never more than one instance of a method so static methods only change the access rights.
Quoted for emphasis. There are perfectly good reasons to use the static modifier, but if you don't know what they are, you should not be using the modifier.

The language is likely to be either C# or Java, by the way.
Kia kaha, Kia māia, Kia manawanui.
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Re: Patterns & Practices : Static Methods

Post by rustypup »

Ron2K wrote:There are perfectly good reasons to use the static modifier, but if you don't know what they are, you should not be using the modifier.
static final is good for the right reasons... public static is mostly bad outside of event-driven/component code...

yet i see it often... :cry:
Most people would sooner die than think; in fact, they do so - Bertrand Russel
RuadRauFlessa
Registered User
Posts: 20576
Joined: 19 Sep 2003, 02:00
Location: Bloodbank

Re: Patterns & Practices : Static Methods

Post by RuadRauFlessa »

rustypup wrote:
Ron2K wrote:There are perfectly good reasons to use the static modifier, but if you don't know what they are, you should not be using the modifier.
static final is good for the right reasons... public static is mostly bad outside of event-driven/component code...

yet i see it often... :cry:
public static works fine with my Singleton patterned Database connectors I write. Although they are more for class factoring than anything else. I don't use them for much more in any case.
:rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock:
Spoiler (show)
Intel Core i7-2600k @ 3.4GHz
Corsair Vengence 2x4GB DDR3 2000MHz
Thermaltake Toughpower 850W
ASUS nVidia GTX560 1GB
CoolerMaster HAF 932
GrimStoner
Registered User
Posts: 716
Joined: 08 Oct 2004, 02:00
Contact:

Re: Patterns & Practices : Static Methods

Post by GrimStoner »

I wrote a couple of classes that model database rows, and then have a dbrow manager class, that is used to retrieve the data from the database, i.e. an Event class, and then an EventManager class, with GetAllEvents, GetFilteredEvents, AddEvent etc. methods. I don't think that I'll be calling these methods other than with my WCF service. So, is it advisable to have to create an instance of each manager class when I want to get data over the service?

Currently I do this... Silverlight app instantiates a service client, and calls the function to retrieve data. The service then instantiates one of the manager classes, and then calls the GetData method on it. The manager class then gets the data from the db, models each row in the datareader with a private function in the manager class (i.e. ModelDataReaderAsEvent), and returns a list of the class retrieved to the service (i.e. List<Event>).

And it's C#.
-Prometheus-
Resident Drama Llama
Posts: 967
Joined: 05 Mar 2008, 02:00
Contact:

Re: Patterns & Practices : Static Methods

Post by -Prometheus- »

........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
BBLounge - Broadband and Technology forum
Please like our facebook page
Post Reply