Access modifiers in Java control the visibility of classes, constructors, methods, and variables. They are essential for encapsulation and security.
Non-access modifiers provide functionality other than access control, such as abstract, static, final, synchronized, etc.
public class Example {
private String name;
public int age;
protected String address;
String phoneNumber; // default access
}
The private modifier is used to restrict access to the members of a class. Members declared private can only be accessed within the same class.
The public modifier provides the highest level of access, allowing members to be accessible from any other class.
Console Output:
Access Modifiers Demonstrated
Static fields and methods belong to the class rather than any specific instance, making them accessible without creating an object of the class.
public class MathUtils {
public static double PI = 3.14159;
public static double square(double number) {
return number * number;
}
}
Static members can be accessed directly using the class name, without needing an instance of the class.
Console Output:
PI: 3.14159
The final modifier is used to restrict inheritance and modification. A final class cannot be extended, a final method cannot be overridden, and a final variable cannot be reassigned.
public final class Constants {
public static final double GRAVITY = 9.8;
public final void display() {
System.out.println("Display method");
}
}
Using final helps in preventing unnecessary changes and ensures consistency in behavior across different parts of the application.
Console Output:
GRAVITY: 9.8
Abstract modifier is used to declare a class that cannot be instantiated or a method that must be implemented by subclasses.
abstract class Animal {
abstract void makeSound();
public void eat() {
System.out.println("Eating...");
}
}
Abstract classes provide a base for subclasses to build on, enforcing a contract for specific methods without providing a full implementation.
Console Output:
Abstract Class Example
The synchronized modifier is used to control access to a method or block by multiple threads, ensuring that only one thread can execute it at a time.
public class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
}
Synchronization prevents thread interference and consistency problems, making it crucial for multi-threaded applications.
Console Output:
Synchronized Method Example
The transient modifier is used in serialization to indicate that a field should not be serialized.
import java.io.Serializable;
public class User implements Serializable {
private String username;
private transient String password;
}
Transient is useful when you want to prevent sensitive data from being serialized, such as passwords.
Console Output:
Password Field Not Serialized
The volatile modifier is used to indicate that a variable's value will be modified by different threads.
public class SharedResource {
private volatile boolean flag = true;
public void toggleFlag() {
flag = !flag;
}
}
Volatile ensures visibility of changes to variables across threads, making it essential for flags and status updates in multi-threaded environments.
Console Output:
Volatile Variable Example
The native modifier is used to declare a method that is implemented in native code using JNI (Java Native Interface).
public class NativeExample {
public native void print();
static {
System.loadLibrary("NativeLib");
}
}
Native methods are used for performance-intensive tasks and to access system-level resources unavailable in Java.
Console Output:
Native Method Example
The strictfp modifier is used to restrict floating-point calculations to ensure portability across platforms.
public strictfp class Calculator {
public strictfp double add(double a, double b) {
return a + b;
}
}
Strictfp is beneficial when consistent floating-point results are needed across different Java platforms.
Console Output:
Strictfp Class Example
An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types.
interface Animal {
void makeSound();
default void eat() {
System.out.println("Eating...");
}
}
Interfaces are used to achieve abstraction and multiple inheritance in Java, allowing different classes to implement the same set of methods.
Console Output:
Interface Example
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies