Public and Void | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Public and Void

public means the visibility....visibility to whom?? Void says method dosent return a value..but without a method main..the program doesn't give output..why is that? and methods must be named..can we give any name instead of main method?? my doubts may be silly but please help me..I like programming

2nd Jul 2016, 4:43 PM
Srivatsa Balaji
Srivatsa Balaji - avatar
7 Answers
0
visibility to other classes. example if you make a class and make its fields (variables ) private you cannot access them directly from the main class for example. but if you make it public you can.
2nd Jul 2016, 4:56 PM
seyfullah
seyfullah - avatar
0
Yes you can name your method any name other than main
2nd Jul 2016, 4:58 PM
Abdelrhman Sami
Abdelrhman Sami - avatar
0
no that is something you cant do. it must be called , public static void main(String [] args ). those are just the rules.
2nd Jul 2016, 5:03 PM
seyfullah
seyfullah - avatar
0
but other methods that you make .you can name it what you want.
2nd Jul 2016, 5:04 PM
seyfullah
seyfullah - avatar
0
you need a good book. try head first Java or the complete Java reference.
2nd Jul 2016, 5:25 PM
Anany Gulati
Anany Gulati - avatar
0
visibility means scope of that class whether it can be accessed from other class or not. if you are a beginner then it will take some time to understand but don't worry you will get it.
3rd Jul 2016, 2:03 PM
Prince Avi
Prince Avi - avatar
0
coming to your second question void: execution always start from main method so main method is not going to return any value. let me show you an example. public void add() { a=1+2; System.out.print(a); } public int add1() { a=1+2; return(a); } in first case we are printing the value in same function but what if we need it in other method in second case we are returning the value you will have to store the value which is returned from the add1() function. so main method is not going to return any value to any function. Think yourself to whom main method should return
3rd Jul 2016, 2:20 PM
Prince Avi
Prince Avi - avatar