Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm trying to implement this JavaFX code where I want to call remote Java class and pass boolean flag:

final CheckMenuItem toolbarSubMenuNavigation = new CheckMenuItem("Navigation");
    toolbarSubMenuNavigation.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override
        public void handle(ActionEvent e)
        {

            //DataTabs.renderTab = toolbarSubMenuNavigation.isSelected();
            DataTabs.setRenderTab(toolbarSubMenuNavigation.isSelected());
            // call here the getter setter and send boolean flag
            System.out.println("subsystem1 #1 Enabled!");
        }
    });

Java class which I want to call:

public class DataTabs
{    
    private static boolean renderTab; // make members *private*
    private static TabPane tabPane;

    public static boolean isRenderTab()
    {
        return DataTabs.renderTab;
    }

    public static void setRenderTab(boolean renderTab)
    {
        DataTabs.renderTab = renderTab;
        tabPane.setVisible(renderTab);
    }

    // somewhere below
    // set visible the tab pane
    TabPane tabPane = DataTabs.tabPane = new TabPane();
    tabPane.setVisible(renderTab);
}

This implementation works but I want to optimize it to use less static variables and objects. Can you tell me which sections of the code how can be optimized?

share|improve this question
add comment

1 Answer

  1. remove all the instances of the static keyword from DataTabs (as well as any internal use of tje DataTabs.<member> prefix.
  2. create an instance of the DataTabs class in your JavaFX code and make it accessible to those parts of the code that use it, either by passing it to constructors and keeping a reference to it, or by passing it as a method parameter.

Step 2 can be difficult and require you to restructure some code, but it's worth it. The problem with static stuff is that it reduces the cohesion of your code while introducing (and encouraging) hard-to-trace coupling, which overall leads to bad maintainability.

It may be annoying that something that used to be simple (just use that static stuff) now requires you to find an answer for the sometimes difficult question "how do I get that instance to where I need it?" - but that question is really "what parts is my code divided into, and how do they communicate?", and if you can't answer that, making any big change to your code will be difficult and error-prone.

share|improve this answer
    
Unfortunately I'm a junior developer and much of the recommendations I don't know how to implement. This is the complete (almost) code pastebin.com/guDWujk5 Would you show me in practice how to optimize it, please? –  Peter Penzov Jun 26 '13 at 10:02
    
@Peter Penzov: it depends on how that code is actually run - I'm not familiar with javaFX. But all you need should be a field of type DataTabs in your Menu class, and have your code use that field. –  Michael Borgwardt Jun 26 '13 at 10:23
    
@Peter Penzov: I think Michael is suggesting that you should use dependency injection (especially in item 2 of his answer), +1 for this. You can find some good ideas on how to use DI in this article: tutorials.jenkov.com/dependency-injection/index.html. Besides this there is a lot of reading material on this topic. –  Giorgio Jun 26 '13 at 19:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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