• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static clarifications required  RSS feed

 
Suren Babu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the follwing is true about static modifier.
A. static can be applied to : instance variables, methods, code Segments and classes.
B. a static method cannot be overridden.
C. inner classes can be both static & private.
D. a static reference cannot be made through non static method or code block.
E. abstract & static both can be applied together to a method.
Could somebody get me the correct answers with detail explanations
Thanks
 
Udayan Naik
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suren.Going through the options...
A.static can be applied to : instance variables, methods, code Segments and classes - TRUE.Well,static can be applied to VARIABLES and not instance variables as u have written.Instance variables are those which belong to a particular Object.The rest are all true.
B. a static method cannot be overridden.- TRUE.Static methods are RE-DEFINED in the subclass.
C. inner classes can be both static & private. - TRUE.
D. a static reference cannot be made through non static method or code block - FALSE.It is vice-versa , ie. a non-static variable cannot be accesed through a static method unless an object reference is present.
E. abstract & static both can be applied together to a method. -FALSE. It is not allowed by the compiler.This is because an abstract method is incomplete.It's definition is provided by the subclass.So if this method is made static,what will i do with that incomplete method ?
Since abstract methods are not allowed to be made static, you will require an object of the class to which that abstract method belongs.And that would be impossible,since u cannot instantiate an abstract class.Thus the only way out is to extend that class and override the abstract method.
Sorry if i have been too articulate in my explanations.



------------------
Come on in !! Drinks are on the house in the Big Moose Saloon !!
[This message has been edited by Udayan Naik (edited November 03, 2000).]
 
Suren Babu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
Great explanations in simple terms ...
 
Ram Jonnalagadda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai,
I think staic methods cann't be overridden to be non-static.
If you override that to be static ,it's ok.
Anyone correct me if i am wrong.
bye
 
SHALINI PATEL
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods cannot be overridden in the subclass
STATIC MEMBERS BELONG TO THE CLASS IN WHICH IT IS DECLARED.....
Yu can override only if the members r inherited, and if static members belong to the superclass, it is not inherited by the subclass, Hence they cannot be overridden......
hope it helps
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!