Hi guys, I'm a newbie to this forum and to programming in general. My assignment is to instantiate three separate household objects and then use get and set methods to change the occupants and income, then have the program print out the number of occupants in each house and the income. So far I've got the Household class working but when I try to use it as a method in TestHousehold, I get the error messages "non-static method cannot be referenced from a static context." This is a beginning class and I'm supposed to keep the code as simple and straightforward as possible.
Here's what I have so far, both classes:
public class Household
{
private int Occupants;
private double Income;
Household()
{
Occupants = 1;
Income = 0.0;
}
Household(int Occ)
{
Occupants = Occ;
Income = 0.0;
}
Household(double Inc)
{
Occupants = 1;
Income = Inc;
}
public int getOccupants()
{
return Occupants;
}
public void setOccupants(int Occ)
{
Occupants = Occ;
}
public double getIncome()
{
return Income;
}
public void setIncome(double Inc)
{
Income = Inc;
}
}
********************************************
public class TestHousehold
{
public static void main(String[] args)
{
Household House1 = new Household();
Household House2 = new Household();
Household House3 = new Household();
System.out.println("The house before using the set methods has " +
Household.getOccupants() + " occupants and an income of " + Household.getIncome());
System.out.println("The first household has " + Household.getOccupants() + " occupants
" + "and the second household has " + Household.getOccupants() + " occupants.");
}
}
Can anyone tip me off as to what I'm doing wrong? Also, this may be a stupid question but can I format currency in java? Thanks in advance for the help.
Household.getOccupants() + " occupants and an income of " + Household.getIncome());
If you look inside your Household class, you will see non of your methods are static, meaning that the class they are within needs to be instantiated before you can utilize them.
The getOccupants method you created inside your Household class is merely a getter and will only return the value of the int Occupants that is stored within the instantiated Household class. In your code it would be House1, House2 or House3 that would called getOccupants() like:
Code:
Household House1 = new Household();
// This would return 1 right away
System.out.println(House1.getOccupants());
House1.setOccupants(4);
// Now this would return 4
System.out.println(House1.getOccupants());
So if you wanted to get the total off all occupants, you would need to create an int, and store the summation of all three of your houses.
I hope all that answers your questions / solves your trouble. If not, let me know what Isn't clear and I can try and clarify more.
EDIT: I just noticed you did the same thing for getIncome(), so everything would also apply to that. Its easy to do with loops and lists if you have gotten that far but if not just utilize what you've learnt thus far.
"static" means a class function, not a object function.
House1.getOccupants() is a object function call.---definiens need not keywords "static"
Household.getOccupants() is a class function call.----definiens need keyword "static"
Bookmarks