answersLogoWhite

0

== == Static method cannot be overwritten because it belongs to the class and not to the Object

If you're asking about overriding or overloading, then yes. Static methods can be overridden and overloaded just like any other methods.

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

TaigaTaiga
Every great hero faces trials, and you—yes, YOU—are no exception!
Chat with Taiga
JudyJudy
Simplicity is my specialty.
Chat with Judy
LaoLao
The path is yours to walk; I am only here to hold up a mirror.
Chat with Lao
More answers

Basically, no.

Static methods are generally called using the class name (Integer.parseInt("3"), not myInteger.parseInt("3"). In this case it is clear that overriding makes no sense. If you were to call a static method from a non-static context (for instance, from another method of the same class), it would always determine at compile-time which method to call - it would not try to determine at run-time to call the same method which was declared in the child class.

The following example demonstrates this:

class Child extends Parent {

public static void someStaticMethod();

}

Parent object = new Child();

object.someStaticMethod(); // will always call Parent.someStaticMethod(), even if Child.someStaticMethod is defined.

For more information, see the linked tutorial.

User Avatar

Wiki User

16y ago
User Avatar

Yes it can be private or static .

For private - If you don't want any other class to use it, declare it private. This is a good choice.

A method should be declared static if it doesn't user instance variables or methods. A static method must use only only parameters, local variables, and static constants, and other static methods in the same class. If the static keyword is omitted, the method will be an instance method.

e.g

private static double convertKmToMi(double kilometers) {

double miles = kilometers * MILES_PER_KILOMETER;

return miles;

} These types of methods are extremely useful as helper methods for certain classes.

User Avatar

Wiki User

16y ago
User Avatar

Yes, we can, as you can see in the next example it is a very common task:

public class Sample {

static Integer num;

public static void main(String[] args) {

num = new Integer(5);

}

}

Inside a static context (as the main method) we are restricted to not use no static instance variables, but we can use and initialized static variables without problem.

User Avatar

Wiki User

14y ago
User Avatar

The static modifier marks a variable or a method as a class member; Which means, the static element is shared across all instances of this class (all the objects that you create of this class).

In the case of static methods this feature don not let the use of instance variables, given that your method no longer belong to any instance.

User Avatar

Wiki User

14y ago
User Avatar

Remember, static means that it occurs once per class. You can't really override a static method; it's implicitly final because of this. You cannot override a static method in a subclass with an instance method, but you can override a static method in a subclass if it's of the same construct and it wasn't declared final in the superclass.

User Avatar

Wiki User

14y ago
User Avatar

Well, static methods are class-methods, not object-methods.

Normally, in OO, you specify for which object you want to invoke a method.

This means that the invocation always needs to carry information on what

object the method is invoked on and what the type of the object is.

This is not necessary for class-methods. Static methods can be invoked

regardless of the fact that you have an object of that type or a derived

type. And since it cannot assume the existence of an object, it also cannot

make use of information regarding overriding of methods, hence it calls the

static method that belongs to the class it belongs to.

User Avatar

Wiki User

13y ago
User Avatar

No.

Usually method overriding is done in the subclass(inheritance), if we make method as static it can be accessed using the class name

User Avatar

Wiki User

14y ago
User Avatar

Add your answer:

Earn +20 pts
Q: Static variable and static methods in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering