• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

"this" in Java

I'm playing around with a personally implemented Linked List stack.  I am reversing the order of the stack structure i'm holding (which is just a list of connected Linked nodes)

For some reason I can't assign "this" to a another object of the same type.  i.e. can't do,

this = tempStack;

Probably silly, but why is that?
0
manish_London
Asked:
manish_London
1 Solution
 
Ashish PatelCommented:
this is static to self object and cannot be reassinged
0
 
malfunction84Commented:
"this" is a reserved keyword that refers to the object on which the current constructor/method is operating.  Perhaps this code can clarify:

class Person {

    String name;

    Person(String s) {
        this.name = s;
        // "this" refers to the new instance created by this constructor
    }

    void greet(Person p) {
        System.out.println("Hi" + p.name + ".  My name is " + this.name + ".");
        // "this" refers to the object on which this method is called.
    }
}

Elsewhere...

Person jack = new Person("Jack");
Person jill = new Person("Jill");
jack.greet(jill); //prints "Hi Jill.  My name is Jack."

When you call jack.greet(...), the "this" in the greet method refers to the Person jack.
0
 
agsuvidhaCommented:
Do
this.tempstackt=tempstack

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
malfunction84Commented:
You can never assign a value to "this."  It's not a variable; it's a keyword which acts like a variable.  The only way to change the current object is to modify its fields, much like agsuvidha suggests.

Do you mind posting your source code?
0
 
manish_LondonAuthor Commented:
The following method does the job but not the nicest way I think.  I'm going to produce a recursive solutionn next.      
                      /**
       * returns a reversed version of this stack, iterative solution
       */
      public LinkedStack reverseMe (){
            //note assignment does not appear to work with this
            LinkedStack tempStack = new LinkedStack();
            LinkedStack tempStack2 = new LinkedStack();
            
            while (!this.isEmpty()){
                  tempStack.push(this.pop());
            }
            
            while (!tempStack.isEmpty()){
                  tempStack2.push(tempStack.pop());
            }            
            
            //now push back into this, in the correct order
            while (!tempStack2.isEmpty()){
                  this.push(tempStack2.pop());
            }            
            
            return this;
      }
0
 
malfunction84Commented:
I'm not sure how you're storing your Nodes, but if you can swap out that field of "this" instance for that field of another instance, you'll be able to reverse the list with a very similar approach:

public class LinkedStack {
    private List<Node> nodes; //or however you store them...

    public LinkedStack reverseMe() {
        LinkedStack reversed = new LinkedStack();
        while (!this.isEmpty) {
            reversed.push(this.pop());
        }
        this.nodes = reversed.nodes;
        return this;
    }

}

Even if you only have a reference to the "head," swapping their heads should work for you:

public class LinkedStack {
    private Node head;

    public LinkedStack reverseMe() {
        ...
        this.head = reversed.head;
        return this;
    }
}

This illustrates what I was saying earlier about modifying fields.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now