• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Q from khalid

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following code is from Khalid Mughal book (Pg 104 in my copy). The correct answers as provided at the back of the book is (a), (b) and (e). Now I know why (a) and (b) are
correct, but why is (e) correct?
-Sam
Q: Given the followng code, which statements can be placed at the indicated position without causing compile error.

Select All valid answers
(a) i = this.planets;
(b) i = this.suns;
(c) this = new ThisUsage();
(d) this.i = 4
(e) this.suns = planets;
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can access the static variable either via objects or through class. this.suns essentially is referring to suns data member only. Inside a static method you cannot access non static variable using this but inside non-static method you can access static variable directly or by use of this(use of this is extraneous)
 
sam pitt
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anshul,
I do understand tha concept . that is why I know (a) and (b) are correct. (e) still does not make sense because this reference is implicit and cannot be modified, also how will it prevent the code from throwing compilation error ?
-Sam

[This message has been edited by sam pitt (edited July 11, 2001).]
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question asks you to find those options which will not cause a compile error when inserted and NOT which will prevent a cause a compilation error.
and yes you are right when you say 'this' cannot be modified..
that is why option 'c' is wrong and is not one among the answers.
in option 'e' you are not modifying 'this' but you are modifying what 'this' points to.
'this' is basically like a pointer which contains an address..so by saying we cannot modify 'this'..it is meant that the address cannot be modified. however we can modify the datamembers stored in that address, and that is what we are doing in option 'e'.
 
sam pitt
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I am missing something. My approach towards the problem was to find out the statement(s) in the options given which will assign a value to the local variable i. Now option (a) and (b) do that but(e) do not do any thing like that isn't it ?
-Sam
 
Rex Rock
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes you are really missing something.
Nowhere in the question is it mentioned that 'choose those options which will assign a value to the local variable i'.
The questions says " choose all those which when inserted will compile without cribbing." no mention of the local variable i.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't suns = planets a valid statement?
Since it is, then this.suns = planets is also valid. As is this.suns = this.planets.

I think I am missing something. My approach towards the problem was to find out the statement(s) in the options given which will assign a value to the local variable i.

The question doesn't ask for that. It asks which are valid statements that can be inserted in the code.
------------------
Co-Moderator of the Programmer Certification Forums
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this.suns does not refer to this instance. this.suns refers to an instance field called suns that is contained by this. this.suns CAN be modified.
 
sam pitt
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou all. I got what I was missing
-sam
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic