0

DIFFERENT CLASSES

package battleship;


public class GameSum extends javax.swing.JFrame {


    public GameSum() {
        initComponents();
        J_time.setText(score);
    }



package battleship;

public class GameScreen extends javax.swing.JFrame {


    protected int score;

    public GameScreen() {
        initComponents();

    }   
private void B_checkActionPerformed(java.awt.event.ActionEvent evt) {                                        

       if (score < getHS(0) && score < hs[1])  {setHS(0, score, nickname);  }
       for (int loop = 0; loop<5; loop++)
       {
       if(score > hs[loop] && score < hs[loop+1]) {setHS(loop+1, score, nickname); }   
       }
    }  
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136

3 Answers3

2

Because your variable is not public, or doesn't have a public getter available.

In Gamescreen just do:

public int getScore() {
    return score;
}

And call this function in GameSum.

By the way, I don't want to sound rude, but if this is your real indentation/code convention, it is really unredeable. Try to indent properly, put the content of a if on a new line if you use braces, or on the same line without braces if it is a one line block. Just my two cents.

Jeremy D
  • 4,787
  • 1
  • 31
  • 38
0

You should lean about access modifiers presidency in Java. You have to create a way to access your variable(create public seter and public getter) or make variable public.

Follow this link to refer about access modifiers.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

Your score variable is protected, which mean it is only can access by (Gamescreen)it childs classes and classes on same package and please go through on this https://stackoverflow.com/a/215505/2291134

Community
  • 1
  • 1
gjman2
  • 912
  • 17
  • 28
  • I started with it as a public variable and it didn't work, anyway the score variable is in the same package so it should be accessible from another class in the same package but it isn't. – David Boulton Sep 11 '13 at 12:59