• 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

Q from valiveru

 
Ranch Hand
Posts: 65
  • 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.
The answer given is B,C,D
But i feel that B is not true.Beoz static methods can be over ridden
[ January 08, 2004: Message edited by: dhana rangu ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Dhana rangu:
Its true that static methods can not be overridden. We can only hide them.
Here is an example to illustrate this.
class A{
static void M1(){
System.out.println("A.M1()");
}
void M2(){
System.out.println("A.M2()");
}
}
class B extends A{
static void M1(){
System.out.println("B.M1()");
}

void M2(){
System.out.println("B.M2()");
}
}
public class StaticMethodTest {
public static void main(String[] args) {
A objA = new B();
objA.M1();
objA.M2();

/* B objB = new B();
objB.M1();
objB.M2();
*/
}
}
Cheers
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods CAN NOT be overridden. No way!
You can have a static method is a subclass with the same signature of a static method in a superclass, but this does not cause on "override" of the method. Rather, you have "hidden" the method from the parent class, not overridden it.
Check out this section of the JLS, §8.4.6 Inheritance, Overriding, and Hiding. You'll find lots of useful info, including this:


If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class.


Note that hiding an overriding are VERY different things.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand why D is true:


D. a static reference cannot be made through non static method or code block.


The phrasing is a little convoluted but the way I read it, it is saying that
a non-static method cannot make a static reference. That would seem to be
false as a non-static method can refer to static members and methods.
A static method cannot refer to non-static methods and members but I don't think that D is making that statement.
What am I missing here?
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Don Wood:
I don't understand why D is true:

The phrasing is a little convoluted but the way I read it, it is saying that
a non-static method cannot make a static reference. That would seem to be
false as a non-static method can refer to static members and methods.
A static method cannot refer to non-static methods and members but I don't think that D is making that statement.
What am I missing here?


Don, what you are saying is correct. A static reference can be made through non static method or code block. Here is a code to support it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic