answersLogoWhite

0


Best Answer

== == 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

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

No.

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

This answer is:
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
Related questions

How do you access the static variable and static methods of a class?

In java we access static variables and static methods without creating objects. i.e.,we can access directly by using classname we can also access static variables and static methods by using objects which are created by using class where the static variables and static methods are available


Are static variable created by giving keyword static in java?

yes bcoz static variables


What is static java most commonly used for?

Static java method is the same as a static variable. They belong to a class and not an object of that class. If a method needs to be in a class, but not tied to an object, then one uses static java.


WAP to show the difference between a variable and static variable?

difference between constant and static variables in java


What are static objects in java?

There is no separate entity as a static object in java. The static keyword in java is used to signify that the member (either a variable or a method) is not associated to an object instance of the class. It signifies the fact that the member belongs to the class as a whole. The words static and objects are opposites of one another so you cannot have a static object. However, you can declare an object as a class level variable which could be referred to as a static object but it will be referred to as a static or class variable and not a static object.


Can a method be static and synchronized?

Yes. In Java methods can be static and synchronized. Static methods access other static members in the class. Static in case of inheritance are treated as non - static. Synchronized methods are those which have dedicated thread attached to it and no other can access until currrent thread leaves the control from it.


Can you interchange the term public and static in java?

The two are different, and independent from one another. A variable can be public, static, both public and static, or neither.


Can you inherit static method in java?

Yes. While it is sometimes considered bad style to override static methods, you can treat them like any non-static methods when it comes to inheritance topics.


What does the 'this' reference do in Java?

Represents the current object (not usable in static methods).


Which one executed first static block or static methods in java?

Static Blocks are always executed first. A static block is executed when your class is charged but a static method is executed only when is called, therefor first the class is charged and then is executed a method.


Can you overload static methods in Java?

Yes you can overload the static method. But it should be avoided because memory to static methods are allocated at the time of class load.....so memory will be wasted if we don't use that methods. Whereas non-static method memory allocation depends on Object


What is public static final variable in java?

-Public to all - Static to access with Class Name - Final to change( constant and not alowed to change) Just use it along with class name. (As implied above, a 'public static final' variable in Java is what other languages would call a Constant. )