Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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>()
        public void handle(ActionEvent e)

            //DataTabs.renderTab = 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;

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

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
  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 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: Besides this there is a lot of reading material on this topic. – Giorgio Jun 26 '13 at 19:48

Your Answer


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.