"this" in Java

Posted on 2007-10-10
Last Modified: 2013-11-23
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?
Question by:manish_London
    LVL 23

    Expert Comment

    by:Ashish Patel
    this is static to self object and cannot be reassinged
    LVL 8

    Expert Comment

    "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) {
   = s;
            // "this" refers to the new instance created by this constructor

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


    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.
    LVL 1

    Expert Comment


    LVL 8

    Expert Comment

    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?

    Author Comment

    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()){
                while (!tempStack.isEmpty()){
                //now push back into this, in the correct order
                while (!tempStack2.isEmpty()){
                return this;
    LVL 8

    Accepted Solution

    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) {
            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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    The viewer will learn how to implement Singleton Design Pattern in Java.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now