Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximum number of instance variables that a Java Class should have? [closed]

Tags:

java

oop

Is it good OO design to have 20-25 instance variables in a JAVA class, with their getter/setters?
All variables are totally independent.
As I am using a class in HBase, which represents a column family, so that column family can have different number of columns for each row. So I have to put this much number of variables in that class. I am very curious to know, How many instance variables and methods should a JAVA class have ideally?

like image 917
Nishu Tayal Avatar asked Dec 13 '22 01:12

Nishu Tayal


1 Answers

Trying to have good OO, it is good to look for the class to have a single responsibility. So if you have so many unrelated variables, it sounds like the class is doing too much.

This would be a "code smell" and you should consider re-factoring into multiple classes.

Its great you are interested in good OO. I recommend reading up on Refactoring and Code Smells.

http://c2.com/cgi/wiki?OneResponsibilityRule would be relative here..

like image 114
WineGoddess Avatar answered Mar 02 '23 00:03

WineGoddess