• 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

static modifier

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
answer is BCD, why?
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jeannie,
I dont know about ans C as I've not used inner classes much but can give you some idea for rest of the options,
ans A: static cant be applied to,
1. instance variables : they are supposed to be different for each instance. e.g. if person1 , person2 are objects of person class then we must have "name" as instance attribute. right??? if we make it static it simply useless :-)
2. instance methods: same reason as above because instance methods will perform some operations on instance attributes which means they might have different results for each instance for which they run. e.g. getName() method in a person class should return a particular person's name. if we make it static it binds to the class and can't make references to instance variables obviously.
Rest of the options can be applied same reasoning.
B. static methods can't be overridden. of course. because static methods are bound to class. we invoke them using ClassName.methodName(). so even if override it doesn't make sense.
D. if we make static reference from a non-static method then it would be a disaster as we will have many instances manipulating the shared data (static members of the class) which will surely fall into synchronization problems.
E. if we have a static method as abstarct then we can't implement that in the child (subclass) when we implement the parent class having that abastract method because when we implement any abstract method in a subclass we actually override the parent's method. if we try to do the same thing with static method it won't allow us doing that as we can't override static methods (as given in option B) :-)) then how do we implement that abstract static method in the subclass???
Hope this helps a bit.
Regards,
Maulin
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A. static can be applied to : variables, methods,
code Segments and classes if they are nested classes. Static
variables and instance variables are two completely different
scopes of variables. static = one per class, instance = one
per instantiated object.

B. a static method cannot be overridden, but it can be hidden.

C. inner classes can be both static & private. Depends on the
definition of inner classes. Most people nowadays use
top-level-nested-class for a static nested class since the
JLS has said that the definition of an inner class is a
nested non-static class.

D. a reference cannot be made to a non-static variable from a
static method or code block. However, any method, static or
not, may access a static variable.
E. abstract & static both cannot be applied together to a
method. abstract means the method must be over-ridden. static
means it cannot be over-ridden.
 
Jeannie Yong
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank guys, it is much clear.
 
Replace the word "snake" with "danger noodle" in all tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic