r/javahelp • • 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.

22 Upvotes

10 comments sorted by

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 as static. 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.

1

u/TonyMacaroni1 Apr 28 '19

Hey Nebu, this is the kind of confirmation I was looking for. I am learning java on my own, so I first started to learn with structural programming and many thanks for stating to use mostly static for the helper methods. This helps me a lot as I was doubting relating this.

Secondly, I've also been looking at OOPS and have made various exercices via mooc.fi and I understand the concepts. Again, I have then the same question. Also there, it seems that public void and public static are the most used helper methods (I mean for simple exercices as I do not have the self-confidence to write extensive code). Is this also correct or how should I see/approach this?

Many thanks!!

4

u/Nebu Writes Java Compilers Apr 28 '19

I don't think "most used" is what you should be focusing on. The different modifiers on methods all have specific purposes, but the distinction between those purposes probably won't be clear at this stage in your learning. I'm saying don't worry about it too much, and just write lots of code and do lots of exercises.

It's only when your programs become much larger and more complex that you'll probably understand the benefits of using visibility modifiers other than public. In particular, you might need to get to the point where you're collaborating on a codebase with other software developers who you don't get to regularly talk to, to see the some of those benefits.

1

u/TonyMacaroni1 Apr 28 '19

Ok, many thanks for your answers, it helps a lot!!!

-1

u/WikiTextBot btproof Apr 28 '19

Structured programming

Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of the structured control flow constructs of selection (if/then/else) and repetition (while and for), block structures, and subroutines.

It emerged in the late 1950s with the appearance of the ALGOL 58 and ALGOL 60 programming languages, with the latter including support for block structures. Contributing factors to its popularity and widespread acceptance, at first in academia and later among practitioners, include the discovery of what is now known as the structured program theorem in 1966, and the publication of the influential "Go To Statement Considered Harmful" open letter in 1968 by Dutch computer scientist Edsger W. Dijkstra, who coined the term "structured programming".Structured programming is most frequently used with deviations that allow for clearer programs in some particular cases, such as when exception handling has to be performed.


[ ^PM | Exclude ^me | Exclude from ^subreddit | FAQ / ^Information | ^Source ] Downvote to remove | v0.28

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

u/[deleted] 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

u/_cjj Apr 28 '19

also package-private, if that's not what you're implying by 'none'

1

u/indivisible Apr 28 '19

aka Default Visibility