Results 1 to 4 of 4
  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Quick 'this' question

    public class testers{
    private int x =0;
    public testers(int x ){
    this.x = this.x +1;
    x = this.x + 2;
    }
    public static void main(String[] args){
    testers app = new testers(5);
    System.out.println(app.x);


    }

    }

    I was just wondering why this always prints 1. I'm confused with the this.x = this.x + 1 line, what does it mean?
    Also in the main method what does app.x mean?

    Thanks very much for your time.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Quick 'this' question

    The parameter int x of your testers method covers the declared variable private int x. So if you want to use the class/object x variable, you have to use the this keyword to distinguish!
    The x variable in the testers method is only visible in the body of the method and app.x shows to the private int x.
    This is only 1, because this.x = this.x +1 -> 0 + 1 = 1
    If you would add a System.out.println(x) in the testers method (at the end) it would print 3 -> 1(the "global x") + 2 = the local x = 3

  3. #3
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Quick 'this' question

    Quote Originally Posted by eRaaaa View Post
    The parameter int x of your testers method covers the declared variable private int x. So if you want to use the class/object x variable, you have to use the this keyword to distinguish!
    The x variable in the testers method is only visible in the body of the method and app.x shows to the private int x.
    This is only 1, because this.x = this.x +1 -> 0 + 1 = 1
    If you would add a System.out.println(x) in the testers method (at the end) it would print 3 -> 1(the "global x") + 2 = the local x = 3
    How does app.x show the private int x, I thought as it was private it can't be accessed by the main.

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Quick 'this' question

    Why? private says that it is visible only in this/own whole class! The main method is declared in the same class, so no problem here. If you would add the main method in another class (e.g. class Starter) then you're right

Similar Threads

  1. quick question
    By biggerthanblue in forum New To Java
    Replies: 2
    Last Post: 04-10-2011, 04:33 AM
  2. Quick Question...
    By FatalSylence in forum New To Java
    Replies: 4
    Last Post: 10-15-2010, 02:38 PM
  3. Quick question
    By sAntA199 in forum New To Java
    Replies: 2
    Last Post: 12-09-2009, 03:01 AM
  4. One last quick question
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 08:53 AM
  5. Quick Question
    By Graeme in forum New To Java
    Replies: 4
    Last Post: 01-08-2009, 08:01 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
  •