self = nil; (OBJECTIVE C) vs this = null (JAVA)

Discussion in 'Mac Programming' started by akabeer1983, Jan 20, 2009.

  1. macrumors newbie

    Joined:
    Jan 7, 2009
    #1
    hai,
    in java, 'this' is a keyword that refers to the object on which the method containing the keyword 'this' is invoked.when we assign any value to 'this'
    this = null;
    it shows compile time error: CANNOT ASSIGN VALU TO FINAL VARIABLE 'this'

    However, in Objective C, if i assign

    self = nil;

    it accepts.
    Can anyone explain what happens as a result of this.

    Example:
    /*_____________main()_______________________*/

    int main()
    {
    id OBJ_Alpha;
    OBJ_Alpha = [[Alpha alloc]init];
    [OBJ_Alpha SomeMethod];
    [OBJ_Alpha PrintMethod]; //is this line equivalent to [nil PrintMethod]?
    }


    /*__________________class Alpha_____________________*/

    @implementation Alpha
    {}

    -(void)SomeMethod
    {
    self = nil;
    }

    - (void) PrintMethod
    {
    printf("I am alive!!!");
    }
    @end


    /*__________________________Example ends_____________*/

    Will the PrintMethod work?
     
  2. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #2
    I Imagine it will clear the pointer without freeing the memory of the object it is pointing at. That could be occassionally useful but I can't think of a case now. Generally you should use dealloc...
     
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    The only time I've seen self=nil used is if something fails in your init method and you want to abort object creation. If you use it anywhere else I'd imagine very bad things will happen to your object and most likely your program.
     
  4. macrumors member

    Joined:
    Sep 17, 2008
    #4
    It's not "abort object creation", the object is already created just not initalized. The self pointer in objc is fundamentally different from that of java.
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    OK, sure I agree with what you are saying: the memory space for the object has been allocated in the alloc call and the basic structure set up in the chained calls via [super init] up to NSObject.
     
  6. macrumors regular

    Joined:
    Apr 29, 2008
    #6
    self and this are both object references. In Java, this is more special, in that the compiler won't allow you to assign to it, so you don't get yourself into trouble. But if you could, the same rule applies to all references: you're changing that reference, not affecting other references nor the object.

    Code:
    void foo(id someReference) {
      id a, b;
      a = someReference;
      b = a;
      a = nil;
      if ( b == someReference ) printf("Not nil\n");
    }
    self is special because it is initialized at the start of a method to point to the object, and it is used to reference instance variables. But otherwise, it is just another local variable.

    So when you assign nil in someMethod, it has no side effects, because you don't do anything after. (If you were to call that method again or any other method, self will be reinitialized.) And it certainly does not affect OBJ_Alpha in main, which still points to the object. So printMethod does get called.
     

Share This Page