Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. key word 'this'

key word 'this'

Scheduled Pinned Locked Moved Java
question
7 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tichaona J
    wrote on last edited by
    #1

    Hi Please excuse me if my question sound trivial as I am a novice: Why do we need the key word 'this'? What does it do?

    N N D J R 5 Replies Last reply
    0
    • T Tichaona J

      Hi Please excuse me if my question sound trivial as I am a novice: Why do we need the key word 'this'? What does it do?

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #2

      You don't need it, but it refers to member variables or methods of a class:

      class Foo
      {
      private int bar;

      void int setBar(int bar)
      {
      this.bar = bar;
      }
      }

      this.bar is the memeber variable, bar is the message attribute.


      Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

      L 1 Reply Last reply
      0
      • T Tichaona J

        Hi Please excuse me if my question sound trivial as I am a novice: Why do we need the key word 'this'? What does it do?

        N Offline
        N Offline
        Neo10101
        wrote on last edited by
        #3

        The keyword 'this' refers to the current class you are in. If you write 'this.' while in a class, you can access all members of that class by IntelliSense. 'this' just means 'the current class'. Sounds logical, yes? public class MyClass { private int age = 18; public MyClass { this.age = 20; } } However, 'this' can be omitted. 'this.age' is the same as 'age' while in the same class. Regards, CSTreval

        1 Reply Last reply
        0
        • T Tichaona J

          Hi Please excuse me if my question sound trivial as I am a novice: Why do we need the key word 'this'? What does it do?

          D Offline
          D Offline
          Dave Doknjas
          wrote on last edited by
          #4

          It's also used to call another class constructor from a constructor (sometimes called 'chained constructor calls'):

          class Foo
          {
          public Foo()
          {
          this(0);
          }
          public Foo(int i)
          {
          }
          }

          David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com

          1 Reply Last reply
          0
          • N Nagy Vilmos

            You don't need it, but it refers to member variables or methods of a class:

            class Foo
            {
            private int bar;

            void int setBar(int bar)
            {
            this.bar = bar;
            }
            }

            this.bar is the memeber variable, bar is the message attribute.


            Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Nagy Vilmos wrote:

            You don't need it

            in fact, you may need this, e.g. to pass "yourself" to another object or class method, say when you want to subscribe to an event or a service. FWIW: I do like VB.NET's Me keyword, it seems more appropriate than this. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            modified on Saturday, October 2, 2010 9:05 PM

            1 Reply Last reply
            0
            • T Tichaona J

              Hi Please excuse me if my question sound trivial as I am a novice: Why do we need the key word 'this'? What does it do?

              J Offline
              J Offline
              jwintermute
              wrote on last edited by
              #6

              It's also used to reference an inner class's outer class - but I wouldn't worry about that yet if you're a novice :-D

              1 Reply Last reply
              0
              • T Tichaona J

                Hi Please excuse me if my question sound trivial as I am a novice: Why do we need the key word 'this'? What does it do?

                R Offline
                R Offline
                RaviRanjanKr
                wrote on last edited by
                #7

                It (this) always refers to the current instances of classes. This function is used in access members of Constructors,instance methods and instance accessors Qualify Member hidden by similar names and to pass objects as a parameter to other methods this keyword is commonly used. To get more details click on the given link Java Tutorial: this Keyword[^]

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups