473,396 Members | 1,968 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,396 software developers and data experts.

static variables in a class

I was looking at some code that someone had written, and they had a class
with all static attributes.

Can someone please enlighten me why someone would do that? Doesn't that
break the rule of encapsulation?

I have always been used to seeing accessors and mutators instead of directly
accessing an attribute to change or
access the variable.

Thanks in advance for any insight.
Sep 10 '05 #1
4 1905
john smith wrote:
I was looking at some code that someone had written, and they had a class
with all static attributes.

Can someone please enlighten me why someone would do that? Doesn't that
break the rule of encapsulation?

I have always been used to seeing accessors and mutators instead of directly
accessing an attribute to change or
access the variable.

Thanks in advance for any insight.

Probably just for scoping reasons. Alternatively, they could have just
used a namespace, but maybe there was some reason to prevent it being
re-opened?

Hope this helps,
-shez-

Sep 10 '05 #2
On 9 Sep 2005 17:15:08 -0700, "Shezan Baig" <sh************@gmail.com> wrote:
john smith wrote:
I was looking at some code that someone had written, and they had a class
with all static attributes.

Can someone please enlighten me why someone would do that? Doesn't that
break the rule of encapsulation?

I have always been used to seeing accessors and mutators instead of directly
accessing an attribute to change or
access the variable.

Thanks in advance for any insight.

Probably just for scoping reasons. Alternatively, they could have just
used a namespace, but maybe there was some reason to prevent it being
re-opened?


Right. I've done that to get around an old compiler that has unreliable
namespace support.

By the way, John, how do static variables break the rule of encapsulation?

The use of accessors and mutators is a good idea, but there are times that you
want to let other entities (especially other classes belonging to a common
library) access your members directly for efficiency or even clarity.
Sep 10 '05 #3
M
It's difficult to comment on that specific code without seeing it, but
most of the cases of static members that I've seen or used were simply
to make the same data accessible to every instance of the class such
as an instance counter or a lookup table.

On Fri, 9 Sep 2005 17:06:31 -0700, "john smith"
<pr**************@charter.net> wrote:
I was looking at some code that someone had written, and they had a class
with all static attributes.

Can someone please enlighten me why someone would do that? Doesn't that
break the rule of encapsulation?

I have always been used to seeing accessors and mutators instead of directly
accessing an attribute to change or
access the variable.

Thanks in advance for any insight.


Sep 10 '05 #4
Dave Rahardja <as*@me.com> wrote:
The use of accessors and mutators is a good idea, but there are times that you
want to let other entities (especially other classes belonging to a common
library) access your members directly for efficiency or even clarity.


I found these articles on why getters/setters (also called
accessors/modifiers and inspectors/mutators) should be avoided. It is
given in a Java context but the principles apply to OO design in C++.

Why getter and setter methods are evil
http://www.javaworld.com/javaworld/j...5-toolbox.html

More on getters and setters
http://www.javaworld.com/javaworld/j...2-toolbox.html

--
Marcus Kwok
Sep 27 '05 #5

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

Similar topics

1
by: James | last post by:
Hello Java NG, I not sure if this is the right NG for this type of question but if not please let me know which is, TIA Any way first off let me say I'm a student and this WAS last weeks lab,...
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
9
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts...
4
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
10
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
16
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions -...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.