Access Method Declarations in Java Programming
Previous | Home | Next |
Access methods are methods used to perform operations on the data contained in an object or on static class variables.
Access methods are declared as following
methodModifiers returnType methodNameAndParameters throwsClause methodBody
methodNameAndParameters Access method names are different from their class names
The method body differs from the constructor body in that it does not allow a constructor call statement.
The modifiers allowed for a method include the public, protected,and private modifiers defined for constructors as well as the final, static, abstract, native, and synchronized modifiers.
The final modifier:- identifies a method that cannot be overridden.
The static modifier:- identifies a class method. Class methods are allowed to access static class variables only.Static methods are final.
An abstract method :-is used to identify a method that cannot be invoked and must be overridden by any non-abstract
subclasses of the class in which it is declared. An abstract method does not have a method body.
Instead, it has a semicolon (;).
A synchronized method :-is a method that must acquire a lock on an object or on a class before it can be executed.
Access methods are declared as following
methodModifiers returnType methodNameAndParameters throwsClause methodBody
methodNameAndParameters Access method names are different from their class names
public class Bike
{
private int cadence;
private int gear;
private int speed;
private int id;
private static int numberOfBike = 0;
public Bike(int startCadence, int startSpeed, int startGear){
gear = startGear;
cadence = startCadence;
speed = startSpeed;
id = ++numberOfBike;
}
public int getID() {
return id;
}
public static int getNumberOfBike() {
return numberOfBike;
}
public int getCadence(){
return cadence;
}
public void setCadence(int newValue){
cadence = newValue;
}
public int getGear(){
return gear;
}
public void setGear(int newValue){
gear = newValue;
}
public int getSpeed(){
return speed;
}
public void applyBrake(int decrement){
speed -= decrement;
}
public void speedUp(int increment){
speed += increment;
}
}
Previous | Home | Next |