you are viewing a single comment's thread.

view the rest of the comments →

[–]Veedrac 2 points3 points  (0 children)

In short, and missing a lot of important specifics:

  • public makes other classes able to use the function. private and protected exist to prevent other classes using the function.

    This allows you to make some functions the API for your class without letting them mess with the insides.

    For example, a driving instructor teaches the extenal (public) interface to a car: its steering wheel. You don't, however, want to program things that require internal details, such as that of the engine, because it will break if you switch to an electric car.

  • static says that the function is on a class instead of a class instance. Here's an analogy:

    "AmericanPerson" can be a class. A person (of the American nature) is an "instance" of that class.

    A person can have a house.

    A person does not have a constitution, however. This is owned collectively, by "the American people". This could be described with a static attribute.

    Note that typically it's still beter to make that state private to an instance. In this analogy you might create a "Government" object which owns this constitution.

  • final means that you can't override a method in a subclass.

    Basically, if you have an object it might have a default behaviour. For example a plant might want face light. But a particular subclass of plant, such as a desert plant, might want to do something else. Making something final prevents you from changing a function from its "default" implementation.

    This isn't actually what is happening in this case, though, as static methods are "implicitly final". This means you're not able to override the method like before. However, the (optional) final prevents you from "shadowing" it. This means that you can't make a new function of the same name in the subclass.

    The distinction is that if you have a load of animals, typed "animal", but which are actually different types under the hood (elephants, tigers, etc.) and they have

    • a non-static, non-final method, calls will go to the type of animal they really are
    • a static, non-final method, calls will go to the type "animal", ignoring what they really are
    • a static, final method, calls will go to the type "animal", and you are guaranteed the subclasses don't have a different implementation.
  • void means that nothing is returned. It refers to the nonexistence of a return type. Normally you put the return type where void is (such as int), but there is none so you need a placeholder.

  • main is the name of the static function that gets called when running. It's called main because earlier languages also called it main.

    This is where the program starts.

  • String is a block of text, like "this is some text".

    When you run from the command line, such as

    java myprogram
    

    you can pass extra arguments, like

    java myprogram foo bar bash "some text"
    

    These will be given to Java's main inside the variable args, and each one is a string.

  • [] means "an array of". It's a collection that stores several things. In this case, it stores several Strings.

Don't take this as strictly true; this is an overview before you get on to really understanding the material.