r/javahelp • u/TonyMacaroni1 • Apr 28 '19
Public void vs Public static void vs Public 'return Type'
Hi all
I am learning Java and am now going through some exercices (abstraction and call funtions) and was wondering the following:
As I have seen so far, Public void & Public static void & Public 'return Type' are the most used methods in Java besides the Public static void main method. Is this correct?
And secondly the difference between the three is to me:
Public void : Used when you don't have to create an object and have no return.
Public static void: Used when you need to create an object in the class itself .
Public 'return Type' (Public int, Public String, Public double): This is used when you need to return something.
Am I correct my thinking as I would like to start to do exercices in which I use an extra method in my code as most developers mention that his is better than writing it all in one class/method....
Thanks for the help.
15
u/dusty-trash Apr 28 '19
You use "void" when you don't want to return anything. This applies to public void, public static void, private void etc.
Static means you do not need an instance of the class (new Object ();).
10
Apr 28 '19 edited Apr 28 '19
public / private / protected - method privacy. Private methods are internal only. Public methods are for everyone. Protected methods are for the class and any subclasses.
static - Added to make it a utility function on the class instead of a method for an instance, which is the default. Static methods may be called without reference to an instance and must not reference the keyword, this.
void / {Type} - the return type. void means no return value. Anything else means return that thing.
Also:
final - means that no subclass may overload this method. Non-final (or extensible) is the default.
abstract - for abstract classes only; means this function is defined by, but not implemented by the class, and needs to be implemented by any subclass. Abstract methods may not be private, static, or final.
5
u/hageldave Apr 28 '19
static for methods that are not assiciated with an instance of the class. void for methods that dont return a result.
Also public is a visibility keyword that implies that the Methode can be usef from whatever package or class you are calling it from.
there is also protected and private and none that can be used for information hiding in programming
2
2
u/Nebu Writes Java Compilers Apr 28 '19
Most of the answers in this thread are correct; I just wanted to give you a bit of advice that may help smooth out your learning experience.
I'm assuming most of the code you've written so far was just in one big
public static void main(String arg[])method, with no other helper methods. If you're trying to learn how to break up your code into smaller methods for organizational purposes, I recommend you don't worry about non-static methods at all. I.e. declare all your helper methods asstatic. This is basically making the move into "structured programming".In order to use non-static methods properly, you'll need to learn object oriented programming, which can be overwhelming if you're not already familiar with structured programming. That's why I recommend you take it one step at a time.