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]
# 1

> please explain the code and output?

Do your own homework.

aniseeda at 2007-7-28 17:04:22 > top of Java-index,Java Essentials,Java Programming...
# 2

You tell us what you think, and we'll give you pointers in the right direction, if you're wrong.

masijade.a at 2007-7-28 17:04:22 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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.

aniseeda at 2007-7-28 17:04:22 > top of Java-index,Java Essentials,Java Programming...
# 4

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 > top of Java-index,Java Essentials,Java Programming...
# 5

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 > top of Java-index,Java Essentials,Java Programming...
# 6

We don't care.

-Kayaman-a at 2007-7-28 17:04:22 > top of Java-index,Java Essentials,Java Programming...
# 7

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 > top of Java-index,Java Essentials,Java Programming...
# 8

> 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.

aniseeda at 2007-7-28 17:04:22 > top of Java-index,Java Essentials,Java Programming...
# 9

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 ==.

masijade.a at 2007-7-28 17:04:22 > top of Java-index,Java Essentials,Java Programming...
# 10

> 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 > top of Java-index,Java Essentials,Java Programming...
# 11

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

georgemca at 2007-7-28 17:04:22 > top of Java-index,Java Essentials,Java Programming...
# 12

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 > top of Java-index,Java Essentials,Java Programming...
# 13

> 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

aniseeda at 2007-7-28 17:04:22 > top of Java-index,Java Essentials,Java Programming...
# 14

> 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?

pbrockway2a at 2007-7-28 17:04:22 > top of Java-index,Java Essentials,Java Programming...
# 15

> 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.

masijade.a at 2007-7-28 17:04:26 > top of Java-index,Java Essentials,Java Programming...