Forums Register Login

public static void, public void and public 'returnType'

+Pie Number of slices to send: Send
can someone please explain when can I use those above. I get confused.
+Pie Number of slices to send: Send
If the method is returning something, then the return type needs to be specified. Otherwise, "void" needs to be specified to indicate that nothing is returned.

If the method should be associated with the class (instead of a particular instance of the class), then the keyword "static" is used. Without "static," the method is an "instance" method. See Java Tutorial.
+Pie Number of slices to send: Send
public static void = a static method which allows public access and returns nothing. This means it is available for anyone to see/use because it is public, it is associated with the class not an instance because it is static, and void always simply means there is no return value for the method.

public void = a non-static method which allows public access and again returns nothing. This means it is available to anyone just as above, but this time because it is not static it is associated with an instance not the class itself. Again because of the void there is no return type.

public 'returnType' = a public method which is showing you that where "returnType" is you may place whatever it is your would like your method to return. If you want it to return a string a method would look like


or if you wanted to return an Integer


Generally it is a good practice to search for the information on your own first prior to posting as each of these come up with tons of information if Googled. Also check out the link in marc's post. Welcome to the Ranch!
Do not set lab on fire. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 32247 times.
Similar Threads
Version conflict
help me understand a exam in 《Java Threads Third Edition》
about { } notation
main
Listener
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:29:03.