What is public in Java?
The public is a keyword in Java that is used for functions as well as variables in a program. Whenever we use the keyword public in front of variables, then the variables are available in methods in which it has not been declared as well. Also, when we declare a method in Java as well, we can use the function in classes where it has not been declared as well. The public is an access modifier in Java. There are also other access modifiers in Java-like private, protected and default. Private keyword in Java is such that once a function is declared as private, then the respective function cannot be accessed in other classes where it has not been defined. An access modifier is basically a restriction of methods, functions and classes in a program that cannot be accessed in other classes or methods.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax of public in java
The syntax of public is shown below. In the below example, the variable word is declared as public inside the class Example. The variable word is available in the functions. The functions are Sum() and average(). So, the variable word is readily available in other functions, although it has not been specifically called in the respective function. In case of a variable that has been declared as private cannot be accessed in other functions or classes. Therefore, in the case where the variable is not allowed to get accessed in other functions, the access modifier private is used.
Code:
class Example { public int hello; public void sum(int a) { } public void average(int a) { } }
Examples of public in Java
Given below are the examples mentioned:
Example #1
Now we see a coding example in which the public access modifier is used, and then it is used to add two numbers. There are two classes. One is the main() in which we create an object of Addition(), and then we call the other class through two numbers. The name of the object created is obj, and it is very useful in calculating the sum. There can also be other functions and classes such as Multiply as well as Divide. The coding example is a simple program where there are two classes.
Code:
class Addition { public int add(int a, int b){ return a+b; } } public class Main{ public static void main(String args[]){ Addition obj = new Addition(); System.out.println(obj.add(50, 50)); } }
Output:
The two numbers which are included are 50 both. When we add the two numbers, we get the final output as 100, as shown. The function used to add the two numbers is add(), and an object is created. We can also create other classes where we can have other functionalities, and we can create that object in the main() of the program.
Example #2
In this coding example, we create an object in the main(). The below program is used to multiply two numbers and produce the multiplication output. There is a single object obj that is created and is used to call two numbers. One is the number that has double as its type, which is used in the same class. In the Hello class, there is a main() which is created and is used to call the square() as well as the number which has its type as double.
Code:
class XYZ{ public double num = 100; public int square(int a){ return a*a; } } public class Hello{ public static void main(String args[]){ XYZ obj = new XYZ(); System.out.println(obj.num); System.out.println(obj.square(10)); } }
Output:
The sample output is a clear depiction of the two variables, which are called inside the main(). First, the number 100, which has double as its type, is shown as well as the square of a number which is there in a function. There is a function square() which shows the square of a number and is created inside another class, and then inside another class, there is a main(), which calls the Square(), and the number is shown as input. The number which is shown as input, in this case, is 10, and the square of the respective number is 100, which is shown in the output panel.
Advantages of public in Java
- Unlike the private access modifier, which is the most restricted modifier, public is used when functions are used to call in classes.
- It is available in classes, package, subclass(within the same package) and subclass(within other packages).
- Also, the functions are also available in other classes.
- Public functionality is used for running programs which has a single main(), and there are multiple other functions that have different functionality, which is called inside the main().
Conclusion
In this article, we see a number of coding examples as well as the various advantages of Public Access Modifier in Java. We also see the other access modifiers such as private, protected and default. The disadvantages of private are also there which has enormous restriction. The accessibility of other access modifiers is also shown in this article in a diagram.
The above is the detailed content of What is public in Java?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
