Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8759407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:47:44+00:00 2026-06-13T14:47:44+00:00

I am new to Java (knowing OOPS concepts as I am ASP.NET member but

  • 0

I am new to Java (knowing OOPS concepts as I am ASP.NET member but not doing Java from last 2 years), creating Tic-Tac-Toe game.

Deleted code here as I am putting my code below.

Here, I am wondering how below lines are working?

game frame = new game();
frame.setVisible(true);

I understood that frame is Object of game Class and initialized by new game() Constructor but not understanding that how .setVisible() method works for this object.

And one more difficulty I am facing is how can I access initialized objects in other classes or methods.


Hello,
I asked previous question because 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:47:45+00:00Added an answer on June 13, 2026 at 2:47 pm

    game extends JFrame, which in turn extends java.awt.Window, so inherits its methods.

    PS. Classes should be named with a capital first letter. Game.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a brand new Java developer (I have been working in asp.net) and
When creating a new Java project in IntelliJ IDEA, the following directories and files
I'm new to java but my experience with Matlab and C trained me to
I think this is a case of me not knowing javascript, but I can't
I am relatively new to Java and over the years I had to solve
Firstly, I am new to jQuery, coming from a Java background, so apologies if
I'm new to Java-based web programming and am trying to learn JSF from the
I'm new to Scala (from Java) and I like to develop TDD/BDD. So before
When I start a new Java project in eclipse, the first popup screen allow
We are starting a new Java EE project and am looking for suggestions regarding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.