0

My code prints 0 instead of size and year. What am I doing wrong? Basically I want it to return those but I'm not sure what I'm doing.

public class House {
private int year;
private int size;
private static int nbrOfHouses; 
public static final int MIN_SIZE =10; 

public House(int _year,int _size){
    _year = year;
    _size = size;
}
public static int getNbrHouses(){ 
    return nbrOfHouses;
}
public int getYear(){
    return year;
    }
public int getSize(){
    return size;
}
}


House[] myHouse = new House[10];{
  myHouse[0] = new House(1902, 120);
  myHouse[1] = new House(1954, 180);
  myHouse[2] = new House(1995,90);

  for(int i=0; i< myHouse.length; i++){
        if(myHouse[i]!=null){
          System.out.println(myHouse[i].getYear());
1
  • you may also use advance for loop for(House house: myHouse){ System.out.println(house.getYear()); } Commented Sep 2, 2013 at 4:54

5 Answers 5

5

This is backwards:

public House(int _year,int _size){
    _year = year;
    _size = size;
}

Should be:

public House(int _year,int _size){
    year = _year;
    size = _size;
}

or better yet:

public House(int year,int size){
    this.year = year;
    this.size = size;
}
Sign up to request clarification or add additional context in comments.

Comments

4

In your constructor, you are not assigned the class variables correctly. Do this:

public House(int _year,int _size){
    year = _year;
    size = _size;
}

You're assigned the arguments to the class variables (which are initialized to 0). Since the class variables are initialized at 0 and are not modified, that's why it prints 0.

As mentioned by @pfrank, Java naming conventions don't usually have the underscore. A more conventional way to code it is with the this keyword.

public House(int year,int size){
    this.year = year;
    this.size = size;
}

Comments

2

use

public House(int _year,int _size){

this.year = _year;
this.size = _size;
}

1 Comment

that wont work, since the arguments of the constructor are _year and _size
1

Fields are not initialized properly.

public House(int _year,int _size){
    this.year = _year;
    this.size = _size;
}

1 Comment

this.year = _year Careful of the argument name
1

The Java Language Specification section 15.26 states

AssignmentExpression:
    ConditionalExpression
    Assignment

Assignment:
    LeftHandSide AssignmentOperator AssignmentExpression

LeftHandSide:
    ExpressionName
    FieldAccess
    ArrayAccess

AssignmentOperator: one of
    = *= /= %= += -= <<= >>= >>>= &= ^= |=

You always assign from the right hand side to the left hand side. This

public House(int _year,int _size){
    _year = year;
    _size = size;
}

is therefore reversed and should be

year = _year;
size = _size;

Also, because instance fields are by default initialized to 0 for primitive types, all your int fields have a value of 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.