Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Any way to restrict class variables only to some methods?

Thread view: 
Shawn - 27 Oct 2006 19:37 GMT
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.
adwords@pulpjava.com - 28 Oct 2006 05:29 GMT
You've got to refactor dude!

That class is doing too much. Figure out what properties and
responsibilities should be logically linked together, and factor those
out into separate classes.

Refactor, dude. Refactor.

-Cameron McKenzie
Author of What is WebSphere?

For Free Java Certification Mock Exams: www.scja.com
Free Java and J2EE Multimedia Tutorials: www.mcnz.com
www.pulpjava.com www.technicalfacilitation.com

> Hi,
>
[quoted text clipped - 41 lines]
>
> Thank you very much.
Dijon Yu - 28 Oct 2006 13:52 GMT
> 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
[quoted text clipped - 3 lines]
> 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.
Lew - 28 Oct 2006 17:32 GMT
>> 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

Pedantic point: these aren't "class variables", they're "instance variables".

- Lew


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.