INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Instance variable scope - Confused

Instance variable scope - Confused

(OP)
I have this piece of code.

public class Q {

static int x = 11;
private int y = 33;

public static void main(String args[]) {
Q q = new Q();

q.call(5, 555555);

System.out.println("Value of x " + q.x);
System.out.println("Value of y " + q.y);

}

public void call(int x, int y) {
Q q = new Q();

this.x = x;
this.y = y;

System.out.println("Output: " + x);
System.out.println("Output: " + y);
System.out.println("----------------------------");

System.out.println("Output: " + Q.x);
System.out.println("Output: " + q.x);
System.out.println("Output: " + q.y); // why this is still producing 33 and not 555555
System.out.println("Output: " + y);
System.out.println("----------------------------");
}

}

It produces following output:

Output: 5
Output: 555555
----------------------------
Output: 5
Output: 5
Output: 33
Output: 555555
----------------------------
Value of x 5
Value of y 555555

I understand everything in above output except see the question as comment in above code?

Thanks,

Al

RE: Instance variable scope - Confused

Hi

There you instantiate 2 objects of class Q, both objects assigned to variables called q. So I colored the 2 objects' references distinctly :

CODE --> Q.java

public class Q {

    static int x = 11;
    private int y = 33;

    public static void main(String args[]) {
        Q q = new Q();

        q.call(5, 555555);

        System.out.println("Value of x " + q.x);
        System.out.println("Value of y " + q.y);

    }

    public void call(int x, int y) {
        Q q = new Q();

        this.x = x;
        this.y = y;

        System.out.println("Output: " + x);
        System.out.println("Output: " + y);
        System.out.println("----------------------------");

        System.out.println("Output: " + Q.x);
        System.out.println("Output: " + q.x);
        System.out.println("Output: " + q.y); // why this is still producing 33 and not 555555
        System.out.println("Output: " + y);
        System.out.println("----------------------------");
    }

} 
See ? call() being a method of class Q, using this inside it refers to itself, regardless how may other objects of the same class were instantiated by that.

Please post your code between [code] and [/code] TGML tags to preserve indentation.

Feherke.
feherke.ga

RE: Instance variable scope - Confused

(OP)
feherke,

Thanks a lot. It makes sense.

In fact, after posting this question, I went and ran this code few times in Eclipse debug mode and stepped through it. It showed me exactly what you have highlighted in your answer i.e. this in a method refers to the calling object itself.

Also, I have a made a note to use TGML tags next time around.

Al



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close