Home arrow static arrow Java Programming [Archive] - Is it good practice to have lots of instance variables?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Is it good practice to have lots of instance variables?
This topic has 5 replies on 1 page.

Posts:104
Registered: 12/1/03
Is it good practice to have lots of instance variables?  
Jul 24, 2004 11:29 PM



 
Hi,
Normally, I put lots of variables at the start of my class, so that every method can access them. But is it a good practice? Will that be considered as bad coding technique?

Thx.
 

Posts:370
Registered: 8/17/02
Re: Is it good practice to have lots of instance variables?  
Jul 24, 2004 11:38 PM (reply 1 of 5)



 
well it depends... how you do it...
1. public variables are bad... hide the variables and make variable access method
UNLESS THEY ARE FINAL!!
2. It can be good and it can be bad... if your looking for speed I would say yes go for the instance variables... tkes under consideration that upon each loading of a class you system will dynamically allocate local variables... this allocation may take a small small small amount of time.. I doubt you will see any significant change in a small system.. On the other hand.. if you have class variables.. they are already loaded at initalization thus... when you execute your methods... they may run very very very slightly faster...
3. One other thing you may want to consider if we are talking about speed is static variables that will exist through out the life of your program... so when ever you allocate objects.. the static variable are always there this can be useful if you wanna share information accross objects.. or you are storing large vectors of information that you do not want to load upon each instance..
4. The bad part about it is that yes... it is sloppy coding... you read any optimization book it will tell you to take all the elegance out of your program and finly tune it for speed.. this can be detremantal to extensability and upgradability of your program... so you must banace the 3 types evenly and carefully.. and if you do.. you should be good.. I personally use all 3 each one has it's own use...
 

Posts:648
Registered: 6/26/00
Re: Is it good practice to have lots of instance variables?  
Jul 25, 2004 12:12 AM (reply 2 of 5)



 
... so you must banace the 3 types evenly and carefully..
Great advice!

One thing I want to add is optimization is not only about speed, is also considering scalability and memory footprint. More unnecessary variables mean larger memory waste, and in term can affect speed and scale.

On the other hand, there is nothing wrong to have lots of member variable if they are the properties of the class and you definitely need them. But a class typically should only need a handful of member variables. If what you mean �a lot� is tens and twenties, I would suggest you to group them with wrapper classes, or collections for better control.

In theory, any class can have only one member variable � a HashMap, which wraps all other members. But that is another extreme, and bad practice too.

--

 

Posts:370
Registered: 8/17/02
Re: Is it good practice to have lots of instance variables?  
Jul 25, 2004 12:17 AM (reply 3 of 5)



 
agreed.... the only way you know what is really good, is to do lots of coding.. and have coders like us mock ur style until u get it right... well at least that is how I did it....
 

Posts:370
Registered: 8/17/02
Re: Is it good practice to have lots of instance variables?  
Jul 25, 2004 12:21 AM (reply 4 of 5)



 
by the way in my last comment I ment, each time you load a method... you have to load new instance variables in that method.. if they are locale... so if you call a method lots and lots of times... in som cases it might be good to make it a class variable... but like my freind here said having a ton of class variables is not always very good design... I personally don't worry about such things... if you wanna squueze max speed out of your system you should not be coding in java...
 

Posts:104
Registered: 12/1/03
Re: Is it good practice to have lots of instance variables?  
Jul 25, 2004 12:26 AM (reply 5 of 5)



 
Oh great! Thx for your advice.
 
This topic has 5 replies on 1 page.