Results 1 to 5 of 5
- 02-02-2010, 09:47 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
this keyword possibly buggy with scopes ?
I have 3 classes, PosManager, Sim, Pos
I have over ridden the .equals method in the Sim class
I have used the "this" keyword , am I using it incorrectly ? Details follow below.
2nd line, is my invocation, instead it invokes the PosManager instance. The PosManager has a Pos which has a Sim, I call the getSim of the Pos object which returns the Sim, then the sim invokes .equals(Sim sim) <<the above method>> where I invoke "this" and it returns the PosManager Object instead. Though, when going in line 3, the methods/attributes are correctly given.Java Code:public boolean equals(Sim sim){ if(!(sim == null && this == null)){ if(this.getId().equals(sim.getId())) return false; else return true; }else if(sim == null && this == null){ return true; } return false; }
I recieved the information of the "this" instantiation being PosManager from eclipse, maybe it is at fault ? However when comparing 2 sims where the ID is the same , they return is if they were not the same.
To me this behaviour is incorrect as the this keyword should be the current instance of the class
Any help would be appreciated (regarding my question)Last edited by Fubarable; 02-02-2010 at 10:50 AM. Reason: code tags added
- 02-02-2010, 10:10 AM #2
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
I do realise that you can't call this method from a null variable of type Sim class it is just a scenario, so if there is an instance of the sim calling this method , "this" behaves incorrectly
Last edited by Coral; 02-02-2010 at 10:26 AM.
-
If you are receiving a compiler or JVM error, what do you honestly believe the chances are that this is due to a bug in Java or Eclipse vs. the chances that it is a bug in your code?I recieved the information of the "this" instantiation being PosManager from eclipse, maybe it is at fault ? However when comparing 2 sims where the ID is the same , they return is if they were not the same.
1) Your equals method is not a true equals since its signature doesn't match that of Object's exactly (the parameter must be Object, not Sim).
2) The "this" object will never be null when this method is successfully called.
3) My equals methods often look similar to this:
Java Code:@Override public boolean equals(Object obj) { if (obj == null) { return false; } if (this == obj) { return true; } if (!(obj instanceof Sim)) { return false; } Sim otherSim = (Sim) obj; // if id is an object and not a primitive. return id.equals(otherSim.getId()); // if a primitive, then // return id == otherSim.getId(); }Last edited by Fubarable; 02-02-2010 at 11:03 AM.
- 02-02-2010, 11:50 AM #4
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
Thanks for the rapid response Fubarable
I understand about the null object as stated, and the rest of your points you have made
Thanks for that.
It was a semantic error that I had made :-/. (forgot an '!')
Yet it is still weird that eclipse tells me that "this" in that context appears as the incorrect class (only for the "watch" window that pops up when hovering over it)
You are welcome to delete this thread as the topic is rather derogatory :P
Sorry about thatLast edited by Coral; 02-02-2010 at 11:52 AM.
-
You're most welcome. I'm glad that you figured it out.
Without more code, it's hard to tell what Eclipse/Java is complaining about.Yet it is still weird that eclipse tells me that "this" in that context appears as the incorrect class (only for the "watch" window that pops up when hovering over it)
What do you mean derogatory? I see nothing inherently bad about this thread.You are welcome to delete this thread as the topic is rather derogatory :P
Sorry about that
Much luck!
Similar Threads
-
help with designing Java program:file browser w/ regex search, possibly media player?
By jmd9qs in forum New To JavaReplies: 0Last Post: 11-04-2009, 09:09 PM -
550 5.7.1 <mhgsss@yahoo.com>... Relaying denied. IP name possibly forged [61.95.195.1
By Rajavel in forum NetworkingReplies: 6Last Post: 04-10-2009, 02:08 AM -
What are Custom Scopes in Spring Framework
By Java Tip in forum Java TipReplies: 0Last Post: 03-31-2008, 10:09 AM -
What are Custom Scopes in SpringFramework
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 12:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks