Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this confusion??

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rancgers,

Consider the code below,


I thought the O/P to be 20 but how come it is 10??
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This refers to the currently executing object.To refer the instance varibale i you need this reference.Though the variable i has been shadowed by declaring int i=20;So you need the reference of the this object to get i's value.
hmmmm i is static.I think we already had a disscussion on this topic.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanjeev,

My question is how to interpret the this.i points to the public static int i =99;. I want to interpret it. Please justify this. I understood the latter part...i.e wa can use the this refernce to point out static instance variables. So please tell me what this.i will point to???
 
Saurabh Vyas
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

this is used to refer to instance variables, even if they are shadowed in the method. Thus if we write

System.out.println(this.i);

It fetches the value of instance variable that was 99 and re-assigned to 10. Thus o/p of above statment would be 10.

But if
System.out.println(i);
If above line would have been there than it would have printed 20 ie the local variable of the method.

Thus when some instance variable is shadowed by a local variable and after that if we want to get the value of instance variable than we have to use 'this' keyword.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,
My interpretation for this.i is
"Give me the value of instance variable (i)(static/non static) defined in the currenttly executing object"
Though static varible has a single copy but is accessible to all the objects..and is part of all the objects.
I am not sure whether you want to listen this answer.
[ November 06, 2006: Message edited by: Sanjeev Kumar Singh ]
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, I got it understood.
 
Prashant kumar Singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i want to pour some light on it.
actually,when we declare any instance or static vriables, then it is treated as global to class. here we have declared i as static. But here we are declaring and initialising value of static i as 99. and again initialising in constructor to 10.but in next line we have again declare and initialise i ti 20.But what we are calling (this.i) it means ,jvm will read in the constructor if there it will get any re-initialisation of that static var then it will print that value.if it fined new re-dec;laration of that static va then it will retur from there and will print the what value will be loaded at class loading time.
What i think i have explained.
regards,
Prashant Kumar Singh
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jothi,
This is a quesion for you.
why

is valid ?
Is

also valid?
[ November 06, 2006: Message edited by: Sanjeev Kumar Singh ]
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because, when you declare multidimensional arrays, you can only specify the no or rows. The no of colums vary, so you must not specify when you declare the array. Am I right???or correct me if I'm wrong.

Thanks Sanjeev.
 
Saurabh Vyas
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both the declarations are valid .
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both are valid
 
Prashant kumar Singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

All are correct?
[ November 06, 2006: Message edited by: Prashant kumar Singh ]
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prashant,
I am afraid the last one is not valid.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic