what is finalmethod & final variable with example?
Answer Posted / aditi
Final method cannot be overridden by subclasses. This is
used to prevent unexpected behavior from a subclass
altering a method.
Example:
public class MyClass {
public final void myFinalMethod() {...}
Final Variable, it can only be assigned to once and only in
the constructor of class if variable is not a reference to
pther object.
Example:
public class Sphere {
public static final double PI = 3.141592653589793; //
this might as well be a constant
public final double radius;
public final double xpos;
public final double ypos;
public final double zpos;
Sphere(double x, double y, double z, double r) {
radius = r;
xpos = x;
ypos = y;
zpos = z;
}
| Is This Answer Correct ? | 10 Yes | 0 No |
Post New Answer View All Answers
What is the requirement of thread in java?
Explain treeset?
What is static in java?
How are the elements of a gridbaglayout organized in java programming?
What is the difference between Java and C++?
Where and how can you use a private constructor?
What do you know about the garbage collector?
Explain all java features with real time examples
What is the preferred size of a component in java programming?
Differentiate between static and non-static methods in java.
What are the four pillars of java?
What is the private method modifier?
How can constructor chaining be done using this keyword?
Is break statement can be used as labels in java?
How does queue work in java?