[Guide] public and private difference

Discussion in 'Programming' started by shieken, Jul 2, 2015.

  1. Hello, if you're particularly new to Java, you may want to read about all the confusing public and private terms on here, and what they mean.

    Ever seen a Java method that looks something like this?

    public void Noob(){}

    This in Java is very common. It's known as a method. So what exactly does it mean?

    Well since this thread is about the difference between public and private in Java, that's what I'll mainly talk about. But knowing what the other things mean is also important.

    I'll first start on what public and private mean in Java. It's pretty simple to explain. Public means that the method (or in some cases variable) is accessible to any class in your project. This means that if the class is imported into another class in your project, they can use that method in the class.

    But what if you don't want it accessible to the other classes? That's when you use private. Private can be used in methods also, as well as variables in some cases. Here's an example:

    private void Noob(){}

    This method as you can see is private. If I turn it into an enum and import it into another class then try to use it, I'll get an error.

    Sometimes you'll hear of protected. Protected is a form of public. It allows the method of variable to be accessed by sub classes only. So how do I know it's a sub class? The class extends another class. Protected also allows access from classes in that package

    Lastly, if there's no indicator, the method can be accessed by classes in that package.

    So now about the whole method thing. I'll put how a method is formatted here:

    <accessibility> <return type> <action>{}

    The accessibility is what I've been yaking about before. Private public and protected. The return type is telling how the method is returned. Here are some common return types:

    Boolean: the return is either true or false

    Void: there is no return

    Finally the action. In plugin making a few common ones would be onEnable() onDisable() or onCommand().

    Well I hope you found this thread helpful.

    -shieken
     
    #1 shieken, Jul 2, 2015
    Last edited: Jul 2, 2015
  2. Missing quite a bit of information here..

    Like, protected fields and methods can also be accessed from other classes inside the same package (but not subpackages). Etc etc.

    Here's a small chart that reiterates the OP's drawn out explanation and fills in the holes pretty well without writing a book.

    [​IMG]
     
  3. Ah thx I haven't done too much research on protected but I will add this part in
     
  4. The only way I see that being correct is if the 'Noob()' was a constructor. But if that was to be declared private, it was raise a whole other discussion about preventing instantiation and why declare the constructor private at all.
     
  5. This was just an example. If it was a two word method, then I would've used camel case
     
  6. But it's not, I made it a method. You can automatically tell it's a method because of the parentheses () and the curly braces {}. And to add to what dot dash said I added in void to make it more clear

    And a constructor can certainly have private in it, in fact it's extremely common. Here's an example you'd put in a multi class plugin:

    private Main instance;
     
  7. Right before you posted this I just added void to it...
     
  8. Constructors are a same format to methods though without a return type.

    @Dotdash that was more a rhetorical question. Though when it comes to singletons better practice is to use enums (which do not take a constructor), as they can be serialised and have ironclad protection against instantiation, whereas the other formats have certain faults.
     
    • Funny Funny x 1
  9. I have no clue why you continue to make these threads. You can't teach what you do not understand or know.

    Stop misinforming people.

    Edit: You also find it difficult to edit your messages... You can simply edit your message and add to what you've posted, without replying every two seconds with a whole new post to boost that post count.
     
    • Agree Agree x 1
    • Funny Funny x 1
    • Winner Winner x 1