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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:00:58+00:00 2026-05-30T15:00:58+00:00

Forgive me, I am new to Java! and is for a university project, I

  • 0

Forgive me, I am new to Java! and is for a university project, I am having trouble with a few concepts, I have tried to google around to no avail!. How can I instantiate a certain amount of heaters of the heater class, inside the MarsRoom class, and then access them in the main of the MasterControlPanel. But depending on the number of numheaters set in the constructor??. I have tried this but its not recognising the object room1.roomheaters[0]. It does however recognise it if I just instantiate one object like Heaters roomheaters = new Heaters();.
Many thanks

public class MasterControlPanel{

    public static void main(String[] args){

    MarsRoom room1 = new MarsRoom(40, 40, 20, 20, 8, 2, 4);
    MarsRoom room2 = new MarsRoom(40, 40, 20, 20, 8, 2, 4);
    MarsRoom room3 = new MarsRoom(40, 40, 20, 20, 8, 2, 4);
    MarsRoom room4 = new MarsRoom(40, 40, 20, 20, 8, 2, 4);
    room1.createheaters();

    System.out.println("Turned " + (room1.roomheaters[0].getHeater() ? " ON" : " OFF"));
    }
}


public class MarsRoom extends Rooms implements RoomInterface{

int roomareasq;
int heatloss;
float insideTemp;
float outsideTemp;
float uvalue;
int numheaters;
int numlights;
Heaters roomheaters[] = new Heaters[numheaters];

public MarsRoom(){
}

public MarsRoom(int windowsH, int windowsW, int wallsH, int wallsW, int windowC, int heaters, int lights){
    windowsHeight = windowsH;
    windowsWidth = windowsW;
    wallsHeight = wallsH;
    wallsWidth = wallsW;
    windowCeiling = windowC;
    numheaters = heaters;
    numlights = lights; 
}

public void createheaters(){
for (int i=0; i < numheaters; i++)
{
roomheaters[i] = new Heaters();
}
}


public void calculateheatloss(){

}

}
  • 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-30T15:01:00+00:00Added an answer on May 30, 2026 at 3:01 pm

    The array roomheaters is never declared, so it’s impossible to populate it, let alone access it as a property of a MarsRoom from another class:

    roomheaters[i] = new Heaters();
    // variables must be declared to be assigned
    

    Have a look at declaring member variablesOracle


    UPDATE

    To access a variable as an object property like room1.roomheaters[0], its must be a member variable. You have only created a local variable that disappears as soon as the function returns. Above link should help.

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

Sidebar

Related Questions

I'm new to java so forgive the noob question. I have created a swing
Forgive me, for I am fairly new to C++, but I am having some
I'm new in Java so please forgive any obscene errors that I may make
Please forgive me if any of this is wrong -I'm completely new to Java.
forgive me i'm new to capistrano we have multiple sites we want to deploy
Hey I am new java so forgive me if what I am about to
Please forgive this rather basic question, but I'm very new to Java and still
Please forgive if i use terminology wrong, but i'm new to java-web development and
Forgive my elementary question as I'm pretty new to cocos2d, but I'm having an
Forgive me, I am new to mysql I have this mysql table: |ID|KeyID1|KeyID2|Param|Value|... |asdf|1|2|

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.