Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the public modifier in java

Shulou Source: shulou.com Published: 2022-05-31 10:53:16 10月03日 Update

This article mainly explains "how to use public modifiers in java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use the public modifier in java.

1. Any other class can access classes, methods, constructors, and interfaces declared as public.

2. If the public classes that visit each other are distributed in different packages, you need to import the package where the corresponding public classes are located. Because of class inheritance, all public methods and variables can be inherited by their subclasses.

Example

Public class demo1 {public static void main (String [] args) {Person p1 = new Person (); p1.fn (); System.out.println (p1.a); / / 100 System.out.println (p1.scorce); / / 12.5 / / System.out.println (p1.abc); / / unable to access error report} class Person {int a = 100 / / visible in the same package without any modifiers. Public float scorce = 12.5f; / / visible to all classes. Using objects: classes, interfaces, variables, methods private double abv = 545.6487485; / / visible within the same class. Public void fn () {System.out.println ("I am the fn function");} at this point, I believe you have a better understanding of "how to use the public modifier in java". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Methods content variables interfaces learning different practical deeper interests functions examples practicality reality objects location simple operation more friends websites channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi vpn Docker Shulou Tech Info Microsoft