Need help on a basic matter
Hello,
I'm trying to build a basic java program to test what i know.
So far, it's not going anywhere.. lol
See my code below, as I have 1 main class called HOUSE, and subclasses called living.. kitchen.. and whatever
I have input variable like lenght and width and trying to "getarea". the result i'm getting is NULL.. and i don'T know why
Any ideas, and thanks for any support you may provide.
/**
*
* @author Daniel
*/
public class AlexHouse1 {
private static int Height = 12; // The height is the same for all rooms
private static int Lenght;
private static int Width;
private static int floors;
private static int HouseNumber = 33; // The House civic number
private static int HouseRooms = 4; // Number of room into the House
private static int YearConstruction = 2001;
private static String HouseStreet = "My street name"; // The Street name
private static String HouseCity = "Toronto"; // City Name
private static String HouseSize;
int temperature; // It's temperature, but i have not assiged value.
String HouseColor = "yellow"; // Outside color of the house
public class AlexHouse1Size {
public int Lenght = 0;
public int Width = 0;
public AlexHouse1Size(int a, int b){
Lenght = a;
Width = b;
}
public int getArea() {
return Lenght * Width;
}
AlexHouse1Size HouseSize = new AlexHouse1Size(12,15);
}
public class LivingRoom extends AlexHouse1 { // Subclaass Living Room
int LivingRoomLenght;
int LivingRoomWidth;
int LivingRoomLHeight = 8;
int LivingRoomSize;
String LivingRoomColor = "beige";
int LivingRoomTemperature;
}
public static void main(String[] args) {
AlexHouse1 House = new AlexHouse1(); // To create AlexHouse "object"
System.out.println("Alex House have a total of " + HouseRooms + " rooms");
System.out.println("and she was built in " + YearConstruction);
System.out.println("The house total height is " + Height);
System.out.println("The house size is " + HouseSize);
}
}
Re: Need help on a basic matter
You're trying to use static variables as member variables. Instead, you should be doing something like:
AlexHouse1 house = new AlexHouse1();
System.out.println("Alex House have a total of " + house.getHouseRooms() + " rooms");
Also, you should be following standard naming conventions- variables and methods start with a lower-case letter. It might not seem like a big deal, but it makes it so much easier to read.
Re: Need help on a basic matter
Ok, I don't know why i put capital letters everywhere.. I corrected that.
with this code:
/**
*
* @author Daniel
*/
public class AlexHouse1 {
private static int height = 12; // The height is the same for all rooms
private static int lenght;
private static int width;
private static int floors;
private static int houseNumber = 33; // The House civic number
private static int houseRooms = 4; // Number of room into the House
private static int yearConstruction = 2001;
private static String houseStreet = "My street name"; // The Street name
private static String houseCity = "Toronto"; // City Name
private static String houseSize;
int temperature; // It's temperature, but i have not assiged value.
String houseColor = "yellow"; // Outside color of the house
public class AlexHouse1Size {
public int lenght = 0;
public int width = 0;
public AlexHouse1Size(int a, int b){
lenght = a;
width = b;
}
public int getArea() {
return lenght * width;
}
// AlexHouse1Size HouseSize = new AlexHouse1Size(12,15);
}
public class LivingRoom extends AlexHouse1 { // Subclaass Living Room
int livingRoomLenght;
int livingRoomWidth;
int livingRoomLHeight = 8;
int livingRoomSize;
String livingRoomColor = "beige";
int livingRoomTemperature;
}
public static void main(String[] args) {
AlexHouse1 house = new AlexHouse1(); // To create AlexHouse "object"
System.out.println("Alex House have a total of " + houseRooms + " rooms");
System.out.println("and she was built in " + yearConstruction);
System.out.println("The house total height is " + height);
System.out.println("The house size is " + houseSize);
}
}
the result is :
run:
Alex House have a total of 4 rooms
and she was built in 2001
The house total height is 12
The house size is null
BUILD SUCCESSFUL (total time: 0 seconds)
I don't get why it's null..
Perhaps i should start from scratch all over.
Re: Need help on a basic matter
Right, that's because you're still using those static variables. Those should not be static. And you should not be accessing them directly. They need to belong to an instance of AlexHouse1.
Re: Need help on a basic matter
Another question:
Where do you assign a value to the variable that is printing out as null?
If you don't give a String variable a value, its value will be null.