Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from mock exam....

 
Tony Sam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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?
 
Simeon Shi
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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.

 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Okay, i got it !
Thanks Simeon and Vikrama !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic