File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q from valiveru

 
dhana rangu
Ranch Hand
Posts: 65
  • 0
  • 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.
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 ]
 
srini v
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Don Wood
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic