equlas method
01: class MyClass {
02: static int maxElements;
03:
04: MyClass(int maxElements) {
05: this.maxElements = maxElements;
06: }
07: }
08:
09: public class Q19 {
10: public static void main(String[] args) {
11: MyClass a = new MyClass(100);
12: MyClass b = new MyClass(100);
13:
14: if (a.equals(b))
15: System.out.println("Objects have the same values");
16: else
17: System.out.println("Objects have different values");
18: }
19: }
please explain the code and output?
[604 byte] By [
anshia] at [2007-11-27 10:20:34]

> please explain the code and output?
Do your own homework.
You tell us what you think, and we'll give you pointers in the right direction, if you're wrong.
> 02: static int maxElements;
> 03:
> 04: MyClass(int maxElements) {
> 05: this.maxElements = maxElements;
One thing I can tell you is it's silly to use this to access a static member.
run the program, there was NO compiler error. So it should exist somewhere.
Where it exists?
What it does?
anshia at 2007-7-28 17:04:22 >

though it is illy to use but both the oblects created have value 100 so it shud b true bt coming as false
anshia at 2007-7-28 17:04:22 >

though it is illy to use but both the oblects created have value 100 so it shud b true bt coming as false
anshia at 2007-7-28 17:04:22 >

> though it is illy to use but both the oblects created
> have value 100 so it shud b true bt coming as false
None of the objects have any value. Static members belong to the class.
Since you have not overridden the equals method, no it does not "must" be true.
Edit: And aniseed has a point as well.
Second Edit: As a matter of fact, with the way you have declared the instances, and the fact that equals has not been overridden, equals "must" return false, as it does. Since, equals, as defined in object will return the result of ==.
> though it is illy to use but both the oblects created
> have value 100 so it shud b true bt coming as false
No, it should be exactly what it is. It's your understanding that's faulty, not Java.
jverda at 2007-7-28 17:04:22 >

The code is several layers of abstraction on top of the bare metal, designed to be readable (after a fashion) by human beings, and used to express intent to a machine
The output is another machine's interpretation of a series of fluctuations in the voltage of an alternating current in the machine to which said intent was expressed (see above)
HTH
so basically static int maxElements; line which is responsible if it is non- static then code would have given o/p as true am i correct?
anshia at 2007-7-28 17:04:22 >

> so basically static int maxElements; line which is
> responsible if it is non- static then code would have
> given o/p as true am i correct?
No
> if it is non- static then code would have given o/p as true am i correct?
No.
What do you think the equals() method does? What class is it defined in? Does that class know anything about maxElements?
> so basically static int maxElements; line which is
> responsible if it is non- static then code would have
> given o/p as true am i correct?
try it, but read all responses first, and try to understand them.