when voidMethod is called, the control simply goes to the calling method which is main method after completing its task.Įven though void method doesn’t return any value still, we can use return statement in void method when we want to stop the further execution. If we try to assign the return value of voidExample to any datatype it will give us the compile-time error. In the above example, we have two methods voidMethod with void as return type and returningMethod with String as return type. The accessibility of other access modifiers is also shown in this article in a diagram.String returnedString = returningMethod() The disadvantages of private are also there which has enormous restriction. We also see the other access modifiers such as private, protected and default. In this article, we see a number of coding examples as well as the various advantages of Public Access Modifier in Java. 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().Also, the functions are also available in other classes.It is available in classes, package, subclass(within the same package) and subclass(within other packages).Unlike the private access modifier, which is the most restricted modifier, public is used when functions are used to call in classes.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. 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. 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. The sample output is a clear depiction of the two variables, which are called inside the main(). 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. One is the number that has double as its type, which is used in the same class. There is a single object obj that is created and is used to call two numbers. The below program is used to multiply two numbers and produce the multiplication output. In this coding example, we create an object in the main(). We can also create other classes where we can have other functionalities, and we can create that object in the main() of the program. The function used to add the two numbers is add(), and an object is created. When we add the two numbers, we get the final output as 100, as shown. The two numbers which are included are 50 both. Therefore, in the case where the variable is not allowed to get accessed in other functions, the access modifier private is used. In case of a variable that has been declared as private cannot be accessed in other functions or classes. So, the variable word is readily available in other functions, although it has not been specifically called in the respective function. The variable word is available in the functions. In the below example, the variable word is declared as public inside the class Example. An access modifier is basically a restriction of methods, functions and classes in a program that cannot be accessed in other classes or methods. 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. There are also other access modifiers in Java-like private, protected and default. ![]() The public is an access modifier in Java. ![]() 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. ![]() 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. The public is a keyword in Java that is used for functions as well as variables in a program.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |