• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

kahlid question

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from page 129
int a; //(1)
static int a; //(2)
int f() {return a;} //(3)
static int f() {return a;} //(4)
which statement is true?
(a) declarations (1) and (3) cannot occur in the same class def.
(b) declarations (2) and (4) cannot occur in the same class def.
(c) declarations (1) and (4) cannot occur in the same class def.
(d) declarations (2) and (3) cannot occur in the same class def.
The answer given is (c), but I also thought it could be (d) Can anyone make this question clear for me?
Thanx in advance
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While a static method can not access an instance field, an instance method CAN access a static field. That is why d is correct.
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Camaron,
A clarification here. Static method are restricted to use ONLY
static and final static fields. However, no such restriction exists for the non-static methods. Hence, ONLY choice (c) is correct. Choice (D) is incorrect.
Hope this clears this issue..
Ravindra Mohan.
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes d is incorrect. I got a bit careless, sorry.
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic