Home  >  Article  >  Java  >  What is public in Java?

What is public in Java?

WBOY
WBOYOriginal
2024-08-30 15:22:18901browse

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:

What is public in Java?

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:

What is public in Java?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Throws Keyword in JavaNext article:Throws Keyword in Java