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 7685139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:11:09+00:00 2026-05-31T19:11:09+00:00

public class Game { public Game( boolean createstage, //For sorting purposes int slength, int

  • 0
public class Game {

    public Game(
        boolean createstage, //For sorting purposes
        int slength, 
        int sheight, 
        boolean createplayer, 
        int plength, 
        int pheight, 
        boolean playersprite,
        BufferedImage psprite, 
        boolean defaultcontrols,
        String pcontrols, 
        boolean test
        ) {
    if(test == true) { //if test is true, test
        new Test();
    }else{ //otherwise create a stage is createstage is true and
        if(createstage == true) {
            StageObj gamestage = new StageObj(slength, sheight);
        }
        if(createplayer==true) {
            PlayerObj player = new PlayerObj(plength, pheight, psprite, pcontrols);
        }
    }
}

public Game() {
    new StageObj(100, 100);
    new PlayerObj(10, 10);
}

public StageObj givestageobj() {
    return gamestage;
}

public PlayerObj giveplayerobj() {
    return player;
}

}

So goes the code of my constructor and two variables designed to return the variables created in the constructor. The problem is that the method giveplayerobj and givestageobj both don’t find the variables gamestage and player. This makes sense, but how do I create the variables in the constructor and then somehow pass them to the giveplayerobj() and the givestageobj() variables so someone could theoretically go Game.giveplayerobj() which returns the playerobj created in the constructor?

Thanks

-JXP

  • 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-31T19:11:10+00:00Added an answer on May 31, 2026 at 7:11 pm

    You need to declare them as class attributes and not inside the constructor for that to work. So, you code should look like what is shown below.

    Changes:

    1. I have added two class variables StageObj andPlayerObj because you have getters for them in place.
    2. Removed the declaration of these two variables from inside the constructor.
    3. Added assignment to the overidden default constructor. (Probably what you were trying to achieve with the default constructor)

      public class Game {
      
      private StageObj gamestage = null;
      private PlayerObj player = null;
      
      public Game(
          boolean createstage, //For sorting purposes
          int slength, 
          int sheight, 
          boolean createplayer, 
          int plength, 
          int pheight, 
          boolean playersprite,
          BufferedImage psprite, 
          boolean defaultcontrols,
          String pcontrols, 
          boolean test
          ) {
      if(test == true) { //if test is true, test
          new Test();
      }else{ //otherwise create a stage is createstage is true and
          if(createstage == true) {
              gamestage = new StageObj(slength, sheight);
          }
          if(createplayer==true) {
              player = new PlayerObj(plength, pheight, psprite, pcontrols);
          }
      }
      }
      
      public Game() {
          gamestage = new StageObj(100, 100);
          player = new PlayerObj(10, 10);
      }
      
      public StageObj givestageobj() {
          return gamestage;
      }
      
      public PlayerObj giveplayerobj() {
          return player;
      }
      
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with the following declaration of the fields: public class Game
public class MyClass { public int Age; public int ID; } public void MyMethod()
public class WrapperTest { static { print(10); } static void print(int x) { System.out.println(x);
I defined the following Sound Class to play Sound in my Pacman Game: public
public class GameLauncher { public static void main(String[] args) { GuessGame game = new
public class Test { public static void main(String[] args) { } } class Outer
public class Item { ... } public class Order { public List<Item> Items ...
public class Address { public string ZipCode {get; set;} } public class Customer {
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class CovariantTest { public A getObj() { return new A(); } public static

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.