Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Static to non-static - Organization

  1. #1
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Static to non-static - Organization

    I'm working on a project, written by another group, that relies heavily on a few static objects. As a result it makes the adaptability of the project pretty...well, static. For my project I want to reorganize the code to allow multiple instances rather than relying on a single static one so I can customize each instance and its behavior.

    It sounds a bit easy, but I hit a problem because so much of the code refers to these static objects. In fact, many of the functions go several methods and objects deep. Imagine the simplistic example where I call method A in the static object A, and it creates object B which creates object C which creates object D, and so on. And all of these created object refer back to A in a static way . The first way that comes to mind to make this change is to pass a reference of A down the line, but is there possibly an easier way to go about it? Any and all help is appreciated....if this even makes sense
    Last edited by copeg; December 4th, 2009 at 09:58 AM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Static to non-static - Organization

    I'm sorry no one has been able to answer you on this one copeg. I would give it a shot but it sounds to me that I would need to spend some time getting to know the system at hand to be able to help you out with a solution.

    // Json

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Static to non-static - Organization

    Thanks Json. I know its not a trivial question, I was just wondering if there was a simple sort of design pattern that I was missing, which evidently there probably isn't. Changing the code turned out to be easier than I thought though, but all the dependencies really effects the reusability of the code.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Static to non-static - Organization

    Glad to hear you worked it out. Sometimes refactoring is a lot easier than you think it will be

    // Json

  5. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Static to non-static - Organization

    You might find that using a framework like Spring can help with the dependency problems - at my workplace, we've found we can easily add some Spring dependency injection to nasty legacy code, making refactoring much simpler, and it also means there's a lot less passing objects deep into method chains.

    The key to this (and also manual refactoring of the kind you are doing) is to make sure that you only ever pass interfaces as dependencies. The discipline of rethinking everything in terms of interfaces often points up design problems that can be fixed or worked around, and once all object parameters are interfaces, the application becomes a lot more flexible.

    To do this kind of refactoring, you do need a good IDE - we use IntelliJ IDEA, but the free ones (e.g. Eclipse) are pretty good on this.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Static to non-static - Organization

    Quote Originally Posted by dlorde View Post
    The key to this (and also manual refactoring of the kind you are doing) is to make sure that you only ever pass interfaces as dependencies. The discipline of rethinking everything in terms of interfaces often points up design problems that can be fixed or worked around, and once all object parameters are interfaces, the application becomes a lot more flexible.
    Thanks dlorde for your input. That is exactly how I did it, using interfaces rather than setting things in stone with objects. In the end it still has dependencies, but things have better adaptability relative to all the object passing.

Similar Threads

  1. quite small (make the changelog non-static, i.e. load its content from a file)
    By Abdallah in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 30th, 2009, 09:00 PM
  2. Static method
    By kalees in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 20th, 2009, 11:10 AM
  3. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM