Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the public modifier in java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report