Java Modifier Types
javaIn Java, modifiers are keywords that are used to specify the visibility and accessibility of classes, fields, methods, and constructors. There are several types of modifiers in Java, including:
- Access modifiers: These modifiers control the visibility and accessibility of classes, fields, methods, and constructors. There are four access modifiers in Java:
public
: This modifier makes the class, field, method, or constructor accessible from anywhere.
protected
: This modifier makes the class, field, method, or constructor accessible from within the same package and
from
subclasses in other packages.
private
: This modifier makes the class, field, method, or constructor accessible only from within the same class.
default
(or no modifier): This modifier makes the class, field, method, or constructor accessible only from within the
same package.
- Non-access modifiers: These modifiers do not control the visibility or accessibility of classes, fields, methods, and constructors, but they provide additional information or behavior. Some common non-access modifiers in Java are:
static
: This modifier is used to declare a class variable or a method as a class method. Class variables and class
methods are shared by all objects of the class and can be accessed using the class name.
final
: This modifier is used to declare a variable, a method, or a class as final. A final variable cannot be
modified,
a final method cannot be overridden, and a final class cannot be subclassed.
abstract
: This modifier is used to declare an abstract class or an abstract method. An abstract class cannot be
instantiated and must be subclassed. An abstract method does not have an implementation and must be implemented by a
subclass.
synchronized
: This modifier is used to declare a method as synchronized. A synchronized method can be accessed by only
one thread at a time.
Here is an example of how to use access and non-access modifiers in a Java class:
public abstract class Shape {
private int x;
protected int y;
public static final int Z = 10;
public Shape(int x, int y) {
this.x = x;
this.y = y;
}
public abstract void draw();
public synchronized void move(int dx, int dy) {
x += dx;
y += dy;
}
}
``
Other Article on Tag java
- - after and before in java date
- - am pm java date format
- - apk minecraft java edition
- - as been blocked by CORS policy Response to preflight request doesn't pass access control check It does not have HTTP ok status
- - bubble sort in java
- - Can I compile and run a spring boot jar from inside another spring boot application
- - convert java date to localdate
- - design patterns in java
- - encrypt by java and decrypt by node
- - exception handling in java