File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes this confusion?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "this confusion??" Watch "this confusion??" New topic
Author

this confusion??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9499
    
    2

Hi Rancgers,

Consider the code below,


I thought the O/P to be 20 but how come it is 10??


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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.


~Sanjeev Singh<br />SCJP 1.5
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9499
    
    2

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

Joined: Sep 02, 2003
Posts: 72
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

Joined: Nov 01, 2006
Posts: 381
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

Joined: Sep 26, 2006
Posts: 9499
    
    2

Thanks guys, I got it understood.
Prashant kumar Singh
Greenhorn

Joined: Nov 04, 2006
Posts: 22
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

Joined: Nov 01, 2006
Posts: 381
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

Joined: Sep 26, 2006
Posts: 9499
    
    2

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

Joined: Sep 02, 2003
Posts: 72
Both the declarations are valid .
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Both are valid
Prashant kumar Singh
Greenhorn

Joined: Nov 04, 2006
Posts: 22
hi

All are correct?
[ November 06, 2006: Message edited by: Prashant kumar Singh ]
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Prashant,
I am afraid the last one is not valid.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: this confusion??