Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Instance variable scope - Confused 1

Status
Not open for further replies.

AlStl

MIS
Oct 2, 2006
83
US
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
 
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:
[b]public class[/b] [COLOR=mediumorchid]Q[/color] [teal]{[/teal]

    [b]static[/b] [maroon]int[/maroon] x [teal]=[/teal] [purple]11[/purple][teal];[/teal]
    [b]private[/b] [maroon]int[/maroon] y [teal]=[/teal] [purple]33[/purple][teal];[/teal]

    [b]public static[/b] [maroon]void[/maroon] [COLOR=orange]main[/color][teal]([/teal][COLOR=orchid]String[/color] args[teal][]) {[/teal]
        [COLOR=orchid]Q[/color] [highlight lightskyblue]q[/highlight] [teal]=[/teal] [b]new[/b] [COLOR=orange]Q[/color][teal]();[/teal]

        [highlight lightskyblue]q[/highlight][teal].[/teal][COLOR=orange]call[/color][teal]([/teal][purple]5[/purple][teal],[/teal] [purple]555555[/purple][teal]);[/teal]

        System[teal].[/teal]out[teal].[/teal][COLOR=orange]println[/color][teal]([/teal][i][green]"Value of x "[/green][/i] [teal]+[/teal] [highlight lightskyblue]q[/highlight][teal].[/teal]x[teal]);[/teal]
        System[teal].[/teal]out[teal].[/teal][COLOR=orange]println[/color][teal]([/teal][i][green]"Value of y "[/green][/i] [teal]+[/teal] [highlight lightskyblue]q[/highlight][teal].[/teal]y[teal]);[/teal]

    [teal]}[/teal]

    [b]public[/b] [maroon]void[/maroon] [COLOR=orange]call[/color][teal]([/teal][maroon]int[/maroon] x[teal],[/teal] [maroon]int[/maroon] y[teal]) {[/teal]
        [COLOR=orchid]Q[/color] [highlight lightgreen]q[/highlight] [teal]=[/teal] [b]new[/b] [COLOR=orange]Q[/color][teal]();[/teal]

        [highlight lightskyblue][b]this[/b][/highlight][teal].[/teal]x [teal]=[/teal] x[teal];[/teal]
        [highlight lightskyblue][b]this[/b][/highlight][teal].[/teal]y [teal]=[/teal] y[teal];[/teal]

        System[teal].[/teal]out[teal].[/teal][COLOR=orange]println[/color][teal]([/teal][i][green]"Output: "[/green][/i] [teal]+[/teal] x[teal]);[/teal]
        System[teal].[/teal]out[teal].[/teal][COLOR=orange]println[/color][teal]([/teal][i][green]"Output: "[/green][/i] [teal]+[/teal] y[teal]);[/teal]
        System[teal].[/teal]out[teal].[/teal][COLOR=orange]println[/color][teal]([/teal][i][green]"----------------------------"[/green][/i][teal]);[/teal]

        System[teal].[/teal]out[teal].[/teal][COLOR=orange]println[/color][teal]([/teal][i][green]"Output: "[/green][/i] [teal]+[/teal] Q[teal].[/teal]x[teal]);[/teal]
        System[teal].[/teal]out[teal].[/teal][COLOR=orange]println[/color][teal]([/teal][i][green]"Output: "[/green][/i] [teal]+[/teal] [highlight lightgreen]q[/highlight][teal].[/teal]x[teal]);[/teal]
        System[teal].[/teal]out[teal].[/teal][COLOR=orange]println[/color][teal]([/teal][i][green]"Output: "[/green][/i] [teal]+[/teal] [highlight lightgreen]q[/highlight][teal].[/teal]y[teal]);[/teal] [gray]// why this is still producing 33 and not 555555[/gray]
        System[teal].[/teal]out[teal].[/teal][COLOR=orange]println[/color][teal]([/teal][i][green]"Output: "[/green][/i] [teal]+[/teal] y[teal]);[/teal]
        System[teal].[/teal]out[teal].[/teal][COLOR=orange]println[/color][teal]([/teal][i][green]"----------------------------"[/green][/i][teal]);[/teal]
    [teal]}[/teal]

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

Please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags to preserve indentation.

Feherke.
feherke.ga
 
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



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top