Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: this in Java for particular example

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question this in Java for particular example

    Hi All,

    I've read some information for this but would like if you explain me why the output is different for this particular example.

    package test03aug;
     
    public class MainClass {
    	public static void main(String[] a) {
    		Exam e=new Exam();
    		System.out.println(e.age);
    		e.modifyVal(e.age);
    		System.out.println(e.age);
    	}
     
    }
     
    class Exam {
    int age;
    void modifyVal(int age){
    age=age+1;
    	System.out.println(age);
    }
    }

    output:
    0
    1
    0


    package test03aug;
     
    public class MainClass {
    	public static void main(String[] a) {
    		Exam e=new Exam();
    		System.out.println(e.age);
    		e.modifyVal(e.age);
    		System.out.println(e.age);
    	}
     
    }
     
    class Exam {
    int age;
    void modifyVal(int age){
    this.age=age+1;
    	System.out.println(age);
    }
    }

    0
    0
    1


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: this in Java for particular example

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Why do you think? Trace the code, paying particular attention to the println() statements and the values of the variables being printed by them. If you want confirmation or correction, post what you're thinking and ask us for input.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Location
    Canada
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: this in Java for particular example

    The this keyword in Java refers to the object executing the code, in this case the Exam object you create. In this context, using this modifies the globally scoped age field in the object, without this you refer to the locally scoped age variable in the modifyVal method.