SoFunction
Updated on 2025-03-08

Detailed explanation of the usage of public keywords in Java

Part 1: What is public keyword?

In Java,publicis an Access Modifier that controls the visibility of elements such as classes, methods, fields, etc. usepublicKeyword-modified elements can be accessed anywhere, i.e. have the maximum access rights.

Part 2: Use of public keywords

Class 2.1 Access permissions

usepublicKeyword-modified classes can be accessed by any other class, whether or not the classes are in the same package. This makespublicClasses become the basis for reuse and extension of other classes.

2.2 Access permissions for methods

usepublicA keyword-modified method can be called anywhere, regardless of whether the caller is in the same package as the class in which the method is located. Such methods are usually provided to externally called interfaces.

2.3 Access permissions for fields

usepublicKeyword modified fields can be accessed and modified anywhere, regardless of whether the visitor is in the same package as the class in which the field is located. However, exposing fields directly is not a good programming practice and encapsulation should be used to protect access to fields.

Part 3: Examples of public keywords

3.1 Class Access Permission Example

// document:public class Person {
    public String name;
    public int age;
    public Person(String name, int age) {
         = name;
         = age;
    }
    public void sayHello() {
        ("Hello, my name is " + name + " and I'm " + age + " years old.");
    }
}
// document:public class Main {
    public static void main(String[] args) {
        Person person = new Person("Alice", 25);
        (); // Output: Hello, my name is Alice and I'm 25 years old.    }
}

3.2 Method access permission example

// document:public class MathUtils {
    public static int add(int a, int b) {
        return a + b;
    }
    public static int subtract(int a, int b) {
        return a - b;
    }
}
// document:public class Main {
    public static void main(String[] args) {
        int result = (10, 5);
        ("10 + 5 = " + result); // Output: 10 + 5 = 15    }
}

3.3 Example of field access permissions

// document:public class Car {
    public String brand;
    public String model;
    public int year;
    public Car(String brand, String model, int year) {
         = brand;
         = model;
         = year;
    }
}
// document:public class Main {
    public static void main(String[] args) {
        Car car = new Car("Toyota", "Camry", 2022);
        ("Brand: " + ); // Output: Brand: Toyota        ("Model: " + ); // Output: Model: Camry        ("Year: " + );   // Output: Year: 2022    }
}

Part 4: The relationship between public keywords and encapsulation

Encapsulation is an important concept in object-oriented programming, which allows us to encapsulate data and behavior in a class and provide access and modification to the data through public methods.publicKeywords are closely related to encapsulation, we usually declare the fields of the class asprivate, and then through the publicgetandsetMethod to access and modify the value of a field.

4.1 Use encapsulation to achieve access control

// document:public class Person {
    private String name;
    private int age;
    public Person(String name, int age) {
         = name;
         = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
         = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
         = age;
    }
}
// document:public class Main {
    public static void main(String[] args) {
        Person person = new Person("Alice", 25);
        ("Name: " + ()); // Output: Name: Alice        ("Age: " + ());   // Output: Age: 25        ("Bob");
        (30);
        ("Name: " + ()); // Output: Name: Bob        ("Age: " + ());   // Output: Age: 30    }
}

Part 5: Notes and best practices for public keywords

5.1 Use public keywords reasonably

When designing classes and interfaces, it should be used reasonablypublicKeywords, try to set the members and methods of the class toprivateorprotected, access through public methods, thereby realizing encapsulation and information hiding.

5.2 Minimize public interfaces

Try to minimize the public interface (public methods) of the class and only expose the necessary methods, which can reduce external dependence on the class and improve the stability and maintainability of the code.

5.3 Try to avoid direct exposure of fields

Direct exposure of fields is not a good programming practice, and access and modification of fields should be provided through encapsulation to protect the internal state of the class.

Part 6: Summary

This article introduces the JavapublicKeywords, including its usage scenarios, sample code, and relationships to encapsulation. By studying this article, readers should bepublicWith a deeper understanding of keywords, it can be used reasonably in actual developmentpublicKeywords, design Java code that is safer, more stable and easy to maintain. At the same time, we also emphasized the importance of packaging and hope that readers can fully consider the packaging design when writing code to improve code quality and maintainability.

This is the end of this article about the detailed explanation of the usage of public keywords in Java. For more related Java public keyword content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!