Should a method called by a constructor be static?

CaffeineToCode

I am making a simple bible reader for a confirmation project, and I have a UI class. The class opens a windows when called by the main class. I just satisfied multiple errors with static keywords, but one thing is left - my prepareGUI method. Should it be declared static? My IDE throws no errors either way.

package input;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class UI extends JFrame {

    private static final long serialVersionUID = 4226151097999382106L;

    private static JFrame mainFrame;
    private static JLabel headerLabel;
    private static JLabel statusLabel;
    private static JPanel controlPanel;

    public UI() {
        prepareGUI();
    }

    private static/* Should that be there? */ void prepareGUI() {
        mainFrame = new JFrame("Holy Bible");
        mainFrame.setSize(700, 500);
        mainFrame.setLayout(new GridLayout(3, 1));
        mainFrame.setLocationRelativeTo(null);
        mainFrame.getContentPane().setBackground(Color.WHITE);
        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });

        headerLabel = new JLabel("", JLabel.CENTER);
        statusLabel = new JLabel("", JLabel.CENTER);

        statusLabel.setSize(350, 100);

        controlPanel = new JPanel();
        controlPanel.setBackground(Color.WHITE);
        controlPanel.setLayout(new FlowLayout());

        mainFrame.add(headerLabel);
        mainFrame.add(controlPanel);
        mainFrame.add(statusLabel);
        headerLabel.setText((String) ReadFile.currentBookData[0]);
        JLabel label = new JLabel("", JLabel.CENTER);
        label.setText((String) ReadFile.currentBookData[2]);
        label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
        controlPanel.add(label);
        mainFrame.setVisible(true);
    }

    public void updateText() {
        headerLabel.setText((String) ReadFile.currentBookData[4]);
        statusLabel.setText((String) ReadFile.currentBookData[4]);
    }

}
Drew Kennedy

The way you "fixed" your error goes against the concept of OOP. static methods and fields are meant to be "properties" of the class itself, where non-static methods and fields are "properties" of an instance (object) of said class.

What you should do is remove the static keyword from your fields and your prepareGUI() method:

//private JFrame mainFrame; you only have one instance, so no point in having a global field
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;

private /*static Should that be there? answer is no. */ void prepareGUI() {
    JFrame mainFrame = new JFrame("Holy Bible");
    (...)
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Should a method called by a constructor be static?

분류에서Dev

What constructor should be called in return statement with curly braces?

분류에서Dev

Dart Named constructor vs Static method 선호하는 것은 무엇입니까?

분류에서Dev

Calling a Method if another Method is called

분류에서Dev

When is toString() method called

분류에서Dev

Should global static arrays be freed?

분류에서Dev

Mocking static Liferay method

분류에서Dev

Static method loop logic

분류에서Dev

Why is the constructor called twice in my program?

분류에서Dev

Reference to non static member function must be called

분류에서Dev

Why are static variables and methods in java called directly?

분류에서Dev

How to Call Static parameterless constructor in structure?

분류에서Dev

In Java is there a name for the object the method is called on?

분류에서Dev

AbstractTableModel setValueAt method not being called

분류에서Dev

Control method called using variables

분류에서Dev

Method reference to anonymous class constructor

분류에서Dev

Override method or pass function in constructor

분류에서Dev

Should Marshal.FreeHGlobal be Called or LocalFree?

분류에서Dev

Should using the Function constructor form a closure?

분류에서Dev

How to stub a method that is called in initialize method

분류에서Dev

when to use static method or simple class method?

분류에서Dev

Java immutability when defining members in a function called by constructor

분류에서Dev

Application onCreate method gets called twice

분류에서Dev

Flutter: Geolocator return the method 'compareTo' was called on null

분류에서Dev

why is mockito not called when executing mocked method?

분류에서Dev

NSInvocation & NSTimer - Method gets called twice

분류에서Dev

Broadcast Receiver "on recieve" method not been called?

분류에서Dev

shouldPerformSegueWithIdentifier called before checking other method issue

분류에서Dev

Get called method name without __call

Related 관련 기사

  1. 1

    Should a method called by a constructor be static?

  2. 2

    What constructor should be called in return statement with curly braces?

  3. 3

    Dart Named constructor vs Static method 선호하는 것은 무엇입니까?

  4. 4

    Calling a Method if another Method is called

  5. 5

    When is toString() method called

  6. 6

    Should global static arrays be freed?

  7. 7

    Mocking static Liferay method

  8. 8

    Static method loop logic

  9. 9

    Why is the constructor called twice in my program?

  10. 10

    Reference to non static member function must be called

  11. 11

    Why are static variables and methods in java called directly?

  12. 12

    How to Call Static parameterless constructor in structure?

  13. 13

    In Java is there a name for the object the method is called on?

  14. 14

    AbstractTableModel setValueAt method not being called

  15. 15

    Control method called using variables

  16. 16

    Method reference to anonymous class constructor

  17. 17

    Override method or pass function in constructor

  18. 18

    Should Marshal.FreeHGlobal be Called or LocalFree?

  19. 19

    Should using the Function constructor form a closure?

  20. 20

    How to stub a method that is called in initialize method

  21. 21

    when to use static method or simple class method?

  22. 22

    Java immutability when defining members in a function called by constructor

  23. 23

    Application onCreate method gets called twice

  24. 24

    Flutter: Geolocator return the method 'compareTo' was called on null

  25. 25

    why is mockito not called when executing mocked method?

  26. 26

    NSInvocation & NSTimer - Method gets called twice

  27. 27

    Broadcast Receiver "on recieve" method not been called?

  28. 28

    shouldPerformSegueWithIdentifier called before checking other method issue

  29. 29

    Get called method name without __call

뜨겁다태그

보관