Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question from mock exam....

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is the question:
Which of the following statements are true?
1) static methods do not have access to the implicit variable called this
2) A static method may be called without creating an instance of its class
3) A static method may not be overriden to be non-static
4) A static method may not be overloaded
i think ans may be 2) and 3) , but the correct ans is 1),2) and 3) , could somobody here explain this?
i think snippet below is correct:
static int i;
static int method(){
return this.i;
}
so why 1) is true?
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,Tony...
have u ever tried the snippet? the compiler will also complain
"non-static variable this cannot be referenced from a static context". just like a static class which can be referred without
the instance of itself,a static method can also be called without the instance of the class containing it.so...a static method is not "bound" to the class.so,the "this" could not be used coz of this specific.

 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Sam:
here is the question:
Which of the following statements are true?
1) static methods do not have access to the implicit variable called this
2) A static method may be called without creating an instance of its class
3) A static method may not be overriden to be non-static
4) A static method may not be overloaded
i think ans may be 2) and 3) , but the correct ans is 1),2) and 3) , could somobody here explain this?
i think snippet below is correct:
static int i;
static int method(){
return this.i;
}
so why 1) is true?


Yes 1,2,3 are true
For confirmation you may try this code

Bye.
Viki.

------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Tony Sam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, i got it !
Thanks Simeon and Vikrama !
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic