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


Please Help Members By Posting Answers For Below Questions

why not override thread to make a runnable? : Java thread

556


What is an interoperable application in java ?

586


Explain a situation where finally block will not be executed?

574


Where are variables stored?

552


What are byte codes?

657






What is an accessor?

1051


What are generic methods?

530


What are green threads in java?

555


What is the difference between the paint() and repaint() methods in java programming?

622


What is internal variable?

538


Can we increase size of array?

568


What is the purpose of default constructor?

579


Is 0 an irrational number?

602


What is the point of polymorphism java?

543


How do you break a loop?

563