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, I'm trying some examples to classify text from the NLTK cookbook and
Good Day, I have created an asp.net web service on my local machine, and
Good Day everyone!! One question.. or two.. Is it possible to use Expression Blend
Good day! I created overloading constructors as follows: public ContactsBean(String firstName, String lastName, String
Good Day, I have a number of binary strings that were created by a
Good Day! I use EF 5 Beta 2 CodeFirst. For my entities first I
Good day to all. I have a site (created with zend framework and smarty)
Good day to all. I have an odd error. I have created a chat
Good day dear colleagues, I decided to move some projects from MySQL to MongoDB
Good day! I created jar file (using Netbeans) and i can't start it. This

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.