Hello,

I am facing this difficulty here : First I'll give code

public class Game extends JFrame {


    private JPanel contentPane;
    //private JPanel settingPane;

    protected JLabel lblPlayerName2; // I am using this one for my testing
    JLabel[] lbls = new JLabel[9];
    private final ButtonGroup buttonGroup = new ButtonGroup();


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Game frame = new Game();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */

    public Game() {
        setTitle("Tic Tac Toe");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 810, 607);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        contentPane.setName("Tic Tac Toe");
.       
.
.
.
.

    for (int i=0; i<lbls.length; i++) {
        lbls[i] = new JLabel(""+(i+1));
        lbls[i].addMouseListener(new AllbtnBehvr());

        lbls[i].setFont(new Font("Trebuchet MS", Font.BOLD, 95));
        lbls[i].setHorizontalAlignment(SwingConstants.CENTER);
        lbls[i].setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panelGameArea.add(lbls[i]);
    }
    .
    .
    .
    .

} // End of Game() 

class AllbtnBehvr implements MouseInputListener{ //Created this class as SubClass of Game Class

        @Override
        public void mouseClicked(MouseEvent src) {
            // TODO Auto-generated method stub

            JLabel b = (JLabel) src.getSource();
            lblPlayerName2.setText(b.getText());


.       }
.
.
.
.
} // End of AllbtnBehvr
}// End of Game Class

Here, I observed that, variable 'lblPlayerName2' and lbls[] defined outside of Game() Constructor, and it can be accessed, but other variables inside Game() cannot be accessed. Here, I want to ask,

How can I access to variables inside Game()? and
How can I access to variables inside Game() if I create class outside Game Class?
Thanks,
Hakoo Desai.

Recommended Answers

All 2 Replies

You need to declare outside the constructor if you want to access the variables in your class.

Remember one basic rule : The scope of any variable is within the block they declared

Ex:- { int a=5;} so variable 'a' cannot be accessed outside that block.

If you want to access the variables in one class from another class, just create the object of the class that you want to use and access the variables using dot(.) operator

I also see

 protected JLabel lblPlayerName2;

since protected is there , you can access the above variable only in the subclass or in same package.

Learn about Acces specifiers. Click Here

Hi Harinath,
Thanks for your reply, so it means,I knew that concept, but I was not sure. if I am declaring all variable outside, and creating subclasses under Main(Here, Game class) class, is that good practise for programming? or should I create them in individual seperate classes?

And one more question,

As I know, constructor is used to initialise class variables, but here why all objects are declared in Game() constructor? (This code is automatically genrated by Eclipse).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.