Any way to restrict class variables only to some methods?

S

Shawn

Hi,

I have a huge class, which has about 30 class variables. And the class
has many methods. Each method doesn't need all the 30 class variables,
only need a subset of them. In another word, say the 30 class variables
can be grouped into 6 groups (say group A, group B, .., group F).

public class MyHugeClass
{
//group A class variables
private double a, b, c, d, e;

//group B class variables
private double w, t;

..

//group F class variables
private double t1, t2,t3;

public void method1() //only need group A and F variables, but it can
access all, unfortunately
{
...//code
}

public void method2() //only need group B and C variables, but it can
access all, unfortunately
{
...//code
}

...//more methods omitted

} //end of class

The method method1() only need group A and group F class variables,
doesn't need others. The method method2() only need group B and group C
class variables, doesn't need others. But since method1() and method2()
all below to the class, so they have access to all the class variables
(from group A to group F), which is more than they need and could
introduce errors. As you can imagine, this is a huge class. More
restrictions, better, because it could force the errors to be detected.

Thank you very much.
 
D

Dijon Yu

Shawn said:
The method method1() only need group A and group F class variables,
doesn't need others. The method method2() only need group B and group C
class variables, doesn't need others. But since method1() and method2()
all below to the class, so they have access to all the class variables
(from group A to group F), which is more than they need and could
introduce errors. As you can imagine, this is a huge class. More
restrictions, better, because it could force the errors to be detected.

All of us are suggest that you should split your class into more.
I think if you can, refactor and refactor again.

If there are some special reason, so you can't to split it.
Maybe you can write some setter method to set a group variables and
just call the method on some method.
You can try do it although it is not a better method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top