Results 1 to 5 of 5
  1. #1
    Coral is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

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

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

    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

  2. #2
    Coral is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

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

    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.

  4. #4
    Coral is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    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 that
    Last edited by Coral; 02-02-2010 at 11:52 AM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Coral View Post
    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 '!')
    You're most welcome. I'm glad that you figured it out.


    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)
    Without more code, it's hard to tell what Eclipse/Java is complaining about.


    You are welcome to delete this thread as the topic is rather derogatory :P
    Sorry about that
    What do you mean derogatory? I see nothing inherently bad about this thread.

    Much luck!

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2009, 09:09 PM
  2. Replies: 6
    Last Post: 04-10-2009, 02:08 AM
  3. What are Custom Scopes in Spring Framework
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-31-2008, 10:09 AM
  4. What are Custom Scopes in SpringFramework
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-28-2007, 12:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •