Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q from valiveru Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q from valiveru" Watch "Q from valiveru" New topic
Author

Q from valiveru

dhana rangu
Ranch Hand

Joined: Oct 01, 2003
Posts: 65
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

Joined: Dec 15, 2003
Posts: 11
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

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Don Wood
Ranch Hand

Joined: Dec 05, 2003
Posts: 65
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

Joined: Jul 21, 2003
Posts: 522
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.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q from valiveru
 
Similar Threads
Static reference
static modifier
Doubt in Overriding Static Method....?
Static
Static clarifications required