Results 1 to 4 of 4

Thread: 'this' question

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

    Default 'this' question

    public class examprep {

    int x = 0;
    public examprep(int x){

    this.x = this.x +1;
    x = this.x +2;




    }
    public static void main(String[] args){

    examprep a = new examprep(3);
    System.out.println(a.x);

    }

    }

    Hi everyone, firstly I was wondering what does a.x mean (in main method), also why does it print 1.

    this.x = this.x + 1

    so this.x = 1

    then x = this.x + 2

    so x = 3 doesn't it?

    What am I doing wrong here? Thanks for your time

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: 'this' question

    There are two x's. (A) one belongs to the class and (B) one is an argument to the constructor.

    Java Code:
    public class ExamPrep {
        int x = 0;  // <------ (A) an instance variable is declared
        
        public ExamPrep(int x) { // <------ (B) a local variable is declared
            this.x = this.x +1;
            x = this.x +2;
        }
    
        public static void main(String[] args){
            ExamPrep a = new ExamPrep(3);
            System.out.println(a.x);
        }
    }
    (A) In any constructor or non static method the instance variable can be referred to as "this.x". Anywhere else it be referred to as "a.x" where a is the instance whose corresponding x is meant. In non static methods we can also refer to it as "x" but only if there is no local variable with that name.

    (B) The local variable x is referred to using "x" but only within the constructor where it is declared. Outside of that constructor this variable cannot be referred to at all.

    -----

    [Edit] Also, please post readable code. The briefest examples should use standard Java coding conventions with respect to case. Use the "code" tags and make proper use of horizontal and vertical spacing.

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

    Default Re: 'this' question

    Quote Originally Posted by pbrockway2 View Post
    There are two x's. (A) one belongs to the class and (B) one is an argument to the constructor.

    Java Code:
    public class ExamPrep {
        int x = 0;  // <------ (A) an instance variable is declared
        
        public ExamPrep(int x) { // <------ (B) a local variable is declared
            this.x = this.x +1;
            x = this.x +2;
        }
    
        public static void main(String[] args){
            ExamPrep a = new ExamPrep(3);
            System.out.println(a.x);
        }
    }
    (A) In any constructor or non static method the instance variable can be referred to as "this.x". Anywhere else it be referred to as "a.x" where a is the instance whose corresponding x is meant. In non static methods we can also refer to it as "x" but only if there is no local variable with that name.

    (B) The local variable x is referred to using "x" but only within the constructor where it is declared. Outside of that constructor this variable cannot be referred to at all.

    -----

    [Edit] Also, please post readable code. The briefest examples should use standard Java coding conventions with respect to case. Use the "code" tags and make proper use of horizontal and vertical spacing.
    Thanks very much for the reply.

    I know what static variables are but what do static methods do?

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: 'this' question

    Basically you can define a method as a static method when it doesn't operate or access any non-static member of the class, such as variables and methods. When you tried to access them the compiler will complain.

    Static methods usually get their data from parameters passes to them and work with these parameters to do something. A good candidate for static method is utility methods that do some calculation, for example. You can look at the java.lang.Math where you can see a lot of static method defined.

Similar Threads

  1. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 04:01 PM
  2. question posted by indissa: library question.
    By Fubarable in forum New To Java
    Replies: 2
    Last Post: 11-18-2011, 01:14 AM
  3. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 12:05 AM
  4. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •