0

I have a question that was in an older test and I need to know the answer for practicing.

We have the following Class:

public class First{
    private int num1 = 0;
    private int num2 = 0;
    private static int count = 0;
    public First(int num){
        this(num,num);
        count++;
        System.out.println("First constructor1");
    }
    public First(int num1, int num2){
        this.num1 = num1;
        this.num2 = num2;
        count++;
        System.out.println("First constructor2");
    }
    public int sum(){
        return num1 + num2;
    }
    public static int getCount(){
        return count;
    }
}

Now we are operating the following orders:

1. First f1 = new First(10);
2. First f2 = new First(4,7);
3. System.out.println("sum1 = " + f1.sum());
4. System.out.println("count = " + First.getCount());
5. System.out.println("sum2 = " + f2.sum());
6. System.out.println("count = " + First.getCount());

I need to write down the lines that will be printed on screen after those 6 lines. I know that after the first 3 lines it should be like this:

First constructor2
First constructor1
First constructor2
sum1 = 20

The only thing that disturb me is, what's the meaning of a line like line #4? is it a method that operate on the class itself instead on an object?

Another question is that in part B we need to re-define the method equals inside the 'First' class (same method that extends Object) so she will be able to compare between the object that the method operates on and another 'First' type object. The method returns true if both num1, num2 are equals.

I thought about something like this:

public class First {
...
...
.
.
.

    public boolean equals (First anotherObj){
        if ((First.num1 == anotherObj.num1) && (First.num2 == anotherObj.num2))
            return true;
        return false;
    } // equals
} // 'First' class

Am I right?

Matin Kh
  • 5,192
  • 6
  • 53
  • 77
Adam
  • 1,944
  • 3
  • 17
  • 19

3 Answers3

1

Yes, getCount() is a static method of your class First which can be called without instantiating any concrete objects. So in your example by using this method you can read the static variable count, which gets increased by 1 whenever the constructor gets called. So if once you created f1 and f2 count will be 2. "count" is a variable shared by all your First instances.

Your equals() method doesn't work since first of all you need to override

public boolean equals(Object obj)

Secondly, num1 and num2 are private so you need a Getter to make them available. Something like:

public boolean equals(Object obj) {
  return num1 == (First)obj.getNum1() && num2 == (First)obj.getNum2();
}

If you override equals you should also override public int hashCode()

Example for hashCode():

public int hashCode() {
  int result = 5;
  result = 31 * result + num1;
  result = 31 * result + num2;
  return result; 

}

(5 and 31 are primes, you can also use e.g. Eclipse to automatically generate this methods).

bibac
  • 449
  • 8
  • 17
  • 1. Can you explain the equals method you wrote ? 2. Why I need to override hashCode() also ? thx – Adam Sep 02 '12 at 08:54
  • The relation of equals() and hashCode() is explained here: http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java – bibac Sep 02 '12 at 12:11
  • The equals method returns true of the specified condition is true. In order to call the getter on the method parameter you have to cast it to your type, e.g. First. The method you're overriding only defines Object as a parameter and Object doesn't know about your specific method/Getter. So we're comparing num1 from the current object (which contains the equals method) to the value of getNum1() from the object given into the method. num1 == num1 && num2 == num2 == true. You don't need the "if" in your method since the expression already returns true/false – bibac Sep 02 '12 at 12:16
  • will I be able to do: public boolean equals(Object obj){ if (obj==null) {return false;} if(obj==this) {return true;} return true; } public int hashCode{ return 0; } – Adam Sep 02 '12 at 12:48
  • I think it's time for you to break out a Java IDE and a debugger and find out what's happening. :) – bibac Sep 02 '12 at 12:55
  • Ok got it :) but can you tell me how to deal with the hashCode() overriding ? tnx – Adam Sep 02 '12 at 12:59
0

For second question, yes you are right. count is counting your call for constructors.On every call it increases. and yes it is not call on object.

ManMohan Vyas
  • 4,004
  • 4
  • 27
  • 40
0

Line 4 means you are simply calling a static method of the class. a static field or method does not need an instance of the class to be accessed, instead, it can be accessed directly with the class name. Note, that your int count is also a static field, and a static method can only access static fields and methods. Also, with each object initiation, the variable count increases, since it is a static field. Static fields are shared fields.

Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97
  • then what should be printed on screen after line 4 ? – Adam Sep 02 '12 at 11:13
  • it would print 3, since with f1, you are calling the first constructor and within it, the second constructor as well; with f2, only second constructor is called. – Victor Mukherjee Sep 02 '12 at 11:16
  • yes, you see, increment of count is associated only with the constructor call, and a constructor is called only at the time of initiation. count increases only with new object initialisation from its class. Even if you reinitialise, say f1 as, f1=new First(20,10); then it again increases count. – Victor Mukherjee Sep 02 '12 at 12:42