简体   繁体   中英

Adding a private field to a class in Java

I have two relevant classes SpaceInvadersApp and HighScoreTableModel

I need to Add a new private field to the SpaceInvadersApp class of type HighScoreTableModel, and initialise this fi eld at the beginning of the SpaceInvadersApp constructor.

This is my attempt I think I am not doing it correctly.

public class SpaceInvadersApp extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private final GamePanel game;

private HighScoreTableModel highScoreTableModel; // unsure about this

final private JMenuItem menuItemGamePause;

/**
 * Create new Space Invaders application.
 * @throws HeadlessException 
 */
public SpaceInvadersApp() throws HeadlessException {

    highScoreTableModel = new HighScoreTableModel("Name", "Score"); //unsure about this

Heres the relevent code from the HighScoreTableModel class

package spaceinvaders.highscores;

import java.util.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class HighScoreTableModel implements TableModel {


private List<String> col1StringList = new ArrayList<String>();
private List<Integer> col2IntegerList = new ArrayList<Integer>();
private String col1Name, col2Name;

private List<TableModelListener> listenerList = new ArrayList<TableModelListener>();

public HighScoreTableModel(String col1Name, String col2Name) {
    this.col1Name = col1Name;
    this.col2Name = col2Name;

Consider looking the packages of this classes, the need to be at the same package or imported.

Another detail is your variable menuItemGamePause , it needs to be initialized since it is final .

final private JMenuItem menuItemGamePause = new JMenuItem();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM