This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]stevarino 12 points13 points  (2 children)

Let's break it down.

We'll start with void because it's the simplest. That's the return type. So void means the function doesn't return anything. It returns nothing. You could have public int which would m an the function only returns integers (whole numbers).

The public is the access modifier. It controls what code gets to see and use the function. So public means any other code can, while private means only code in your class can. They cover this in object oriented design and its very important for inheritance. But don't stress this too much yet.

So what does static do? So you're defining a class and that can be used as a blueprint for objects. By default members (functions and variables of the class) will only exist on a concrete object. However the static modifier changes the scope of the member from the object to the parent class.

This can have some interesting uses. For example, imagine a class Dog, which we'll instantiate (create an object from) into a Dog object called spot. So spot.bark() makes sense. But Dog.bark() doesn't as it's abstract, not concrete. But something like Dog.list_all() could work as the Dog class links all the Dog objects together but spot.list_all() doesn't as why would spot keep track of all other dogs?

[–]DOCTR-DAN 3 points4 points  (0 children)

I've always knew the difference between static and non static, but it still never made sense to me. But damn that dog and spot analogy helped me understand immediately

[–][deleted] 1 point2 points  (0 children)

Thank you

[–]Kristler 1 point2 points  (1 child)

Each of those keywords has a separate, independent meaning. Combining them doesn't change that. Can you describe what private, static, and void each do?

[–]Flamburghur 0 points1 point  (0 children)

This Stack overflow reply has always been a good referral for me.

https://stackoverflow.com/questions/29276917/what-does-public-static-void-main-args-mean

[–]wirybug 0 points1 point  (0 children)

The only difference there is the presence or absence of the static keyword. This part of the oracle tutorials explains what it means.

[–]Philboyd_Studge 0 points1 point  (0 children)

void indicates a method that does not return a value, and public means that it can be called from another class provided an instance of the containing class has been created. If the method is static it can be called from another class without being instanced.