Modifiers in Java
How modifiers are used in the Java program
Modifiers allow programmers to control the behavior of classes, object constructors, methods, variables, and even blocks of code. Combinations of modifiers may be used in meaningful ways. Modifiers are applied by prefixing the appropriate keyword for the modifier to the declaration of the class, variable or method. The utility of some of these modifiers may not be immediately apparent. However, later in the tutorial, we will see situations where they are necessary..
public static methodX {
- methodX may be accessed anywhere using the class name rather than an instance
private int x;
- This integer may be accessed only within its class
synchronized doSomething {
- Concurrent calls to this method are executed one by one.
abstract class justSignature
- Contains empty methods; just for reference, instances cannot be created
Important Java Modifiers and Descriptions
|
abstract |
|
|
class |
Contains empty methods; objects of an abstract class cannot be created. |
|
interface |
All interfaces are abstract, the 'abstract' keyword is optional |
|
method |
Only declaration and type. Abstract methods can only belong to abstract classes |
|
final |
|
|
class |
Prevents subclasses. Final classes are a 'leaf' in the hierarchy |
|
method |
Cannot be overridden by subclass methods |
|
field |
Cannot change its value. 'Static final' fields are constants. |
|
variable |
Cannot change its value. |
|
private |
|
|
method or field |
Accessible only within the defining class; anything outside cannot access the field. |
|
protected |
|
|
method or field |
Accessible only within its package and its subclasses |
|
public |
|
|
class |
Full Access everywhere |
|
interface |
Full Access everywhere |
|
member |
Full Access everywhere |
|
static |
|
|
class |
The method becomes a 'class' method called through the class name rather than through an instance of the object |
|
method or field |
Method becomes a 'class' method invoked through the class name (e.g. System.out.println) rather than an instance of the object. Even if new instances of the defining object are created, only one instance of the method or field exists |
|
synchronized |
|
|
method |
Only one process can execute such a method at one time; No other process can access the class or change any object within the method. |
· Look at the Signature of the Fruit class. The signature of a method, class or variable refers to its declaration complete with modifiers, input parameters, and output type.
public class Fruit {
· The public modifier is called an access modifier because it defines where the class may be used (accessed). Other access modifiers are private, protected, and default. The default modifier is assumed when no access modifier is specified. In such cases, the class is accessible only within its own package. The public modifier allows any class to access the object and its methods. The protected and private modifiers may only be used for methods and variables. The protected modifier allows only the class and sub-classes to access the method or field (unconnected classes and superclasses cannot access the object), and the private modifier does not allow outside access at all. Private methods and fields may be used to hide the implementation or inner working of a class from the outside. Since the 'if' statement that checks the size of the input color array and accordingly sets the colors of the object appears four times, incorporating a private method that implements the 'if' statement and simply calling the method as follows makes the code less repetitive. Also, declaring this method private prevents developers who misunderstand its purpose from calling the method from their code and attempting to set the size of the colors array of an existing fruit object.
Private Method to Replace Repeating If
private int setArraySize(String[] color) {
int i = color.length;
if i <= 10
{
return i;
}
else
{
return 10;
}
}
Alternate Code in a Constructor
int copyLength; this.name = name; copyLength = setArraySize(color); System.arraycopy(color,0,this.color,0,copyLength); this.image = image ; this.calories = calories ;