Two Objects

If I make two objects of the same class like:

JMenu menu=new JMenu();

JMenu menu= new JMenu("Menu 1");

and then did compared them equal to each other, would they be the same object?

And I'm actually have a program which makes multipe of the same object by calling

new frames such a

frame=new JFrame() and I'm wondering would each fram be different if set to .equals()?

[413 byte] By [blackmagea] at [2007-11-27 7:28:53]
# 1

It's going to depend on a per-class basis. Each class has the right to override equals(), and the obligation to do so if the meaning of equality for that class demands it.

I would suppose, however, that no two different JFrames would be .equal() to each other, because it doesn't really make any sense.

Note that you can also use == to compare objects, and it can be useful to do so at times. It's just that usually .equals() makes more sense.

paulcwa at 2007-7-12 19:09:01 > top of Java-index,Java Essentials,Java Programming...
# 2
JMenu menu=new JMenu();JMenu menu= new JMenu("Menu 1");<nitpick>That code would generate a compile error. Variables have the same name.</nitpick>
floundera at 2007-7-12 19:09:01 > top of Java-index,Java Essentials,Java Programming...
# 3

> and then did compared them equal to each other, would they be the same object?

Write the code and try it!

> frame=new JFrame() and I'm wondering would each fram be different if set to .equals()?

Again you have a specific example so write the code and try it. I see no need to post a question like this until you have tried it. If ,once you try it, you don't understand the results, then you have a SSCCE to post and ask for an explanation.

That is the proper way to ask a question. Make an effort on your own first before posting a "what if" question.

camickra at 2007-7-12 19:09:01 > top of Java-index,Java Essentials,Java Programming...
# 4

I know it would, that was a bad example.

else if(e.getActionCommand().equals(newMeta)){

meta=new CreateMetaSheet();

meta.passMenu(MenuBar.this);

}

Thats a better one. If that action command was called twice, make two objects, which make JFrames, would those two be equals with .equal()?

And I generally do make code and try it myself, but something like this I want to ask first so I don't end up writing 200 lines of code in 5 hours to find out it doesn't work. Better to build on a wheel than re-invent it, right?

Message was edited by:

blackmage

blackmagea at 2007-7-12 19:09:01 > top of Java-index,Java Essentials,Java Programming...
# 5

When that snippet of code is executed an object is created and meta is "pointed" at it. When it gets executed the second a second object is created and now meta is "pointing" at the second one and not the first. Unless you have some other variable also "pointing" to the first object then there is no way for your to compare them.

All that aside, if you want to compare two objects then you will have to override the equals method and only you can decide what "equals" means.

floundera at 2007-7-12 19:09:01 > top of Java-index,Java Essentials,Java Programming...
# 6

> so I don't end up writing 200 lines of code in 5 hours

So you are saying that your time is more valuable than ours because you are too lazy to spend 15 minutes writing a SSCCE to test your assumption or design idea. And what about waiting around an hour waiting and hoping that someone actually answers the questions. Don't you think your time could be better spent creating the SSCCE.

Thats why you write a SSCCE. If you truly understand what you are asking then you should be able to write a quick test in 20-30 lines of code in most of the cases.

In this case the test code would be three lines of code:

meta1=new CreateMetaSheet();

meta2= new CreateMeatShee();

System.out.println( meta1.equals(meta2));

I can't believe you don't know how to write a simple test like this.

Message was edited by:

camickr

camickra at 2007-7-12 19:09:01 > top of Java-index,Java Essentials,Java Programming...
# 7

I have my answer but thats not what I meant. I know that meta1 and meta2 are two different objects. What I was asking b4 if both had the same name and created, would they both be the same object.

And no, I'm not saying anyone times is more valuable here. If someone had asked me a question that I spent hours trying to figure out and finally got the answer, I would help save them time by atleast giving an answer. Is there forums for help? Shouldn't you be less critical/deamaning and more positve/helpful?

blackmagea at 2007-7-12 19:09:01 > top of Java-index,Java Essentials,Java Programming...
# 8

> What I was asking b4 if both had the same name and created, would they both be the same object.

Well they both can't have the same name. A variable can only point to one object at a time, so I don't understand your question.

And you need two variables to compare objects. So given that you know if you assign the object to two different variable they will not be equal, I don't understand what you where asking.

So thats why you create a post a SSCCE. To show code that does what you try to explain in english.

> Shouldn't you be less critical/deamaning and more positve/helpful?

You still haven't learned to respond to postings when you receive suggestions as id evidenced by this posting two days ago:

http://forum.java.sun.com/thread.jspa?threadID=5181968&messageID=9709306#9709306

I've tried to give you the benefit of the doubt but you still refuse to do even the simplest things that have been asked of you.

camickra at 2007-7-12 19:09:01 > top of Java-index,Java Essentials,Java Programming...