7

Whenever I write a new class, I use quite a ton of class variables to describe the class's properties, up to the point where when I go back to review the codes I've typed, I see 40s to 50s of class variables, regardless of whether they are public, protected, or private, they are all used prominently throughout the classes I've defined.

Even though, the class variables consists of mostly primitive variables, like booleans, integers, doubles, etc., I still have this uneasy feeling where that some of my classes with large amounts of class variables may have an impact on performances, however negligible they may be.

But being rational as possible, if I consider unlimited RAM size and unlimited Java class variables, a Java class may be an infinitely large block of memory in the RAM, which the first portion of the block contains the class variables partitions, and the rest of the block contains the addresses to the class methods within the Java class. With this amount of RAM, the performance for it is very nontrivial.

But that above isn't making my feelings any easier than said. If we were to consider limited RAM but unlimited Java class variables, what would be the result? What would really happen in an environment where performance matters?

And probably may get mentioned beforehand, I don't know if having lots of class variables counts as bad Java practice, when all of them are important, and all classes have been refactored.

Thanks in advance.

3
  • Try to reduce class variables if possible. Just changing your design pattern may help in this.
    – Ved
    Aug 8, 2012 at 8:41
  • Are they class variables or constants (i.e. static final). Also would you be able to give an example of a "useful class variable", as I find it a bit difficult to imagine what kind of scenario we're talking about here.
    – beny23
    Aug 8, 2012 at 8:44
  • This is an insane number of variables. If at all, this should be necessary exceedingly rarely. If you find yourself using this more often, your design needs improving. Aug 8, 2012 at 14:58

7 Answers 7

19

Performance has nothing to do with the number of fields an object has. Memory consumption is of course potentially affected, but if the variables are needed, you can't do much about it. Don't worry too much about performance. Make your code simple, readable, maintainable, tested. Then, if you notice performance problems, measure and profile to see where they come from, and optimize where needed.

Maintainability and readability is affected by the number of fields an object has though. 40 to 50 fields is quite a lot of fields, and is probably an indication that your classes do too much on their own, and have too many responsibilities. Refactoring them to many, smaller subclasses, and using composition would probably be a good idea.

3
  • 4
    +1 Maintainability and readability are almost always more important than performance. In fact I would argue that clean, simple, well maintained code will usually perform well if not be the fastest. Aug 8, 2012 at 8:47
  • 3
    JB it does from the point of view of the GC. I work on a trading platform and sometimes we had to put lots of fields into one class to reduce the ammount of time that minor GC takes (we use the CMS collector).
    – Augusto
    Aug 8, 2012 at 8:47
  • I like it. +1 Make sure requirements are met first, then worry about performance if it so happens to become an issue. Aug 8, 2012 at 14:01
5

I hope I don't sound like an ass, but in my view having more than 10 properties in a class is usually a hint of a bad design and requires justification.

Performance wise, if you very often need all those properties, then you're going to be saving some memory, as each object also has a header. So intead of having 5-10 classes you put everyting into one and you save some bytes.

Depending on which garbage collector you use, having bigger objects can be more expensive to allocate (this is true for the CMS garbage collector, but not for the parallel one). More GC work = less time for your app to run.

Unless you're writing a high traffic, low latency application, the benefits of having less classes (and using less memory) is going to be completely overwhelmed by the extra effort needed for maintenance.

1
  • 2
    +1 I don't see class design as much different from db design. Sometimes it's ok and even prefered let a class be a little inflated (2nd and 1st normal form for db analogy), especially if the class is on the ridge of the requirments scope. Aug 8, 2012 at 14:06
3

The biggest problem I see in having a class with a lot of variables is Thread safety - it is going to be really hard to reason about the invariants in such a case. Also reading/maintaining such a class is going to be really hard.

Of course if you make as much as you can fields immutable, that is going to be a lot better.

I try to go with : less is better, easier to maintain.

1

A basic principle we are always taught is to keep cohesion high (one class is focusing on one task) and coupling low (less interdependency among classes so that changes in one doesnot effect others).

While designing a system, I will believe the focus should be more on maintainable design, performance will take care of itself. I don't think there is fixed limit on number of variables a class can have as a good practice, as this will strictly depend on your requirement.

For example, if I have a requirement where the application suggest a course to student, and algorithm needs 50 inputs (scores, hobbies etc), it will not matter whether this data is available in one class or multiple, as the whole information needs to be loaded in the RAM for a faster execution.

I will again say, take care of your design, it is both harmful to keep unnecessary variables in a class (as it will load non-required information to RAM) or split into more classes than required (more references and hence pointer movement)

1

1. I always use this as a thumb of rule. A Class should have only One reason to Change, so It should Do only One Thing.

2. Keeping this in mind i take those variables which are needed to define this class's attributes.

3. I make sure that my class is following the Cohesive principle, where the Methods within the class reflects the Class name.

4. Now after sorting everything out, if i need some other variables to work-out my class, then i need to use them, i have no choice...Moreover after all these thinking and work going into creating a class, will be hardly effected by some additional variables.

0

Sometimes class variables are used as static final constants to store some default strings like product name, version, OS version, etc. Or even to store product specific settings like font size, type, etc. Those static variables can be kept at class level.

You can also use HashMap instead of simple class if you just want to store fields constants or like product setting that rarely change. That may help you speed you response time.

0

Two things I would like to mention : 1. All instance variables are stored in Heap area of RAM.. 2. All static variables are stored in non Heap area(Method area to be specific).

Whatever be the type of variable(instance or static), ultimately all reside in RAM.

Now coming to your question. As far as instance variable is concerned, java's built-in Garbage collector will work, in most cases well and truly effectively, to keep freeing memory. However, static variables are not garbage collected.

If you are highly concerned with memory issues due to large number of variables in your class, you can resort to using Weak References instead of traditional strong reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.