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

  • Home
  • SEARCH
  • 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 3871826
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:51:40+00:00 2026-05-19T21:51:40+00:00

Good day! I created two classes namely Setting and Game; In my game access

  • 0

Good day!

I created two classes namely Setting and Game; In my game access the Setting class first.

In my setting class, I call the setter method from Game which is .setDifficulty. and assign a value to it, example == 2.

public class Setting extends javax.swing.JDialog {

       public Setting (JFrame owner) {
                super(owner, true);
                initComponents();
                setSize(400, 250);
                setLocation(370, 250);
                getContentPane().setBackground(new Color(128, 201, 20));
            }
         private void btnOkMouseClicked(java.awt.event.MouseEvent evt) {                                   
            dispose();
            MainGame m2 = new MainGame(this);
            m2.setDifficulty(jComboBox1.getSelectedIndex());
        }           

Then I access My second CLass which is the game. But I cannot get the value of the difficultLvl outside the setter method. (See my comments on the code)

     public class Game extends javax.swing.JDialog {
        private int difficultLvl = 0;

        public Game(JFrame owner) {
            super(owner, true);
            initComponents();
            setSize(500, 500);
            setLocation(300, 120);
            getContentPane().setBackground(Color.getHSBColor(204, 204, 255));
            System.out.println(difficultLvl);  //SHOULD BE == 2, but == 0;
        }


        public void setDifficulty(int Difficulty) {
            this.difficultLvl = Difficulty;
            System.out.println(difficultLvl); == to 2 which is correct...
        }

The problem is that I cannot access the difficultLvl value outside the setter class… It returns to its default assigned value which on this case is 0. What am I doing wrong? How can access the value inside the setter method. I used this.difficultLvl but with no result. I am just new in java… Please help! Your help would be highly appreciated.
Thank you.

  • 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-05-19T21:51:40+00:00Added an answer on May 19, 2026 at 9:51 pm

    I see a couple of problems.

    First are you sure to instantiate MainGame in the Setter class? Is it a subclass of Game or something different? If the code is correct, difficultLvl' inMainGamehas nothing to do withdifficultLvlinGame` – both are differen classes.

    Second if you want the difficulty level for a game, either do it with the constructor:

     public Game(int difficultyLevel) {
       this.difficultyLvl = difficultyLevel;
     }
    

    or with the setter method, but then you set the value after creating the object and, because we all can’t look into the future, you’ll see nothing but the initial value with your actual code.

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

Sidebar

Related Questions

Good day all, for running multiple threads concurrently is it advisable to create different
Good day. I'd like to ask a question. Why TextBox control Txt in this
Good day to all. I have the following setup: A page with html, js,
Good afternoon, We are building a prototype of a deduper. We are using a
I'm just starting to get into backbone.js. It looks like it's pretty involved and
For school, i have to develop a Twitter client with ASP.NET. In the app,
Possible Duplicate: How to concatenate a number to a variable name in MATLAB? Hi
In a discussion with a peer, it was brought up that we should consider
I have an WPF application which has a local rdlc report which show receipt
I have a coworker that is against type inference in C#. I believe most

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.