Minecraft Blogs / Tutorial

Java Text Tutorial 8 - Method Modifyers

  • 374 views, 1 today
  • 1
  • 0
  • 7
Cool_Man7's Avatar Cool_Man7
Level 16 : Journeyman Modder
9
And we're back! Welcome to episode 8 of Java Text Tutorials!


Table of Contents:
8. What is a method modifyer?
     8.1 public
     8.2 private
     8.3 static
     8.4 Comments




8. What is a method modifyer?
A method modifyer, in simplest possible terms, modifyes a method. When applying a method modifyer to a method, you can sometimes change the output or not make it work properly. You need to know what you are doing when using a method modifyer, especially when we get to the more complicating ones. The most common ones are, public and void. When creating a normal method, you will almost always need these two. However under certain circumstances will you not use these. When I explain what they each do, it will maek more sense.


8.1 public
What does public mean? Well simply, it means that all classes can look at your method and use it. We havn't reached that point yet, but we will very soon. Basically, when declaring a 'normal' method, you will likely have to use this. In methods, visibility when declaring them is vital for a proper method (Except in interfaces, but that's another tutorial). In all essential terms, you need this for a method to work properly. This will throw an exception:

void anotherMethode() {

}

However this will not:

public void anotherMethod() {

}

Get it now? You need public for a proper method. There are exceptions, but those are more complicated topics.

8.2 private
This works exactly the same as public. However other classes cannot use this method. It is 'private' to just that class. You cannot use the method in another class. This is the only difference. Here is an example:

private void anotherMethod() {

}

8.3 static
Static is different. It is not something that modifyes how a class can view it.  It affects the method by making it have only one instance of it. You cannot have many anotherMethod() going around. It is only there once. You cannot reference static methods with non-static methods (except through objects). This would not work:

public void anotherMethod() {
     anotherStaticMethod();
}

public static void anotherStaticMethod() {

}

What this is doing is calling the method anotherStaticMethod(), however since anotherStaticMethod() is static, it would not work. However this would work:

public void anotherMethod() {
     TutorialTwo tutorialTwo = new TutorialTwo();
     tutorialTwo.anotherStaticMethod();
}

public static void anotherStaticMethod() {

}

Assuming they are in different classes. More on this in another tutorial.

8.4 Comments
Comments allow you to take notes in your code. Or they can tell another person reading your code what it means/does. Comments are incredibly useful for many things. As comments do not get comipled, they do not take up extra space. This makes it easier to understand what the code does if you come back to it another time or someone else reviews it. In Java, there are two ways to do comments.
The first is:
// This is a comment
The second is:
/*
* This is a comment.
*/

I prefer the second one, as it is much more noticeable. I use it to leave notes or explain what the code means/does!

In the next tutorial we will talk about multiple classes.
Tags

Create an account or sign in to comment.

Lambdastic
10/11/2014 3:39 pm
Level 7 : Apprentice Warrior
Lambdastic's Avatar
These are modifiers in general. They can also be used with variables and classes.

Having no modifier will not generate an error. the public modifier and the default (no modifier) can both be used by other classes and other packages. The only difference is that the public modifier can be used by subclasses and by the world.

Other than that, I appreciate what you are doing. More people should learn how to program.
1
Cool_Man7
10/14/2014 4:32 pm
Level 16 : Journeyman Modder
Cool_Man7's Avatar
Yes, they are. However I wanted to just cover methods and do variables in another tutorial. This keeps from too much information from flowing in during one session (Some people have actual problems with this). I try to keep it short and simple but too the point with as much as I can on one topic.

I see your point, but as I stated earlier - I don't want too much in one tutorial. I stated 'normal' methods are like this. For now we will only cover methods with public, private, and when we reach further into the series - protected. I was origionally also going to include protected and final and all of that but two things. 1) I was busy at the time and needed to get a tutorial up so this series doesn't stretch on forever (that is why later tutorials experience a lack of photos). 2) I find these (default, protected) less used (by me anyway) and I have not had too much experience with them. This will all be covered in a later tutorial. I try to explain things from simplest to more complicated while trying to put detail into them.

Sorry if I sound rude, but I get what you mean. Thanks and glad you like it! I agree I find programming much fun.
1
SierraHawk
10/03/2014 10:00 am
Level 1 : New Zombie
SierraHawk's Avatar
For beginners it's perfect, good luck in the sequel.
1
Cool_Man7
10/03/2014 9:56 pm
Level 16 : Journeyman Modder
Cool_Man7's Avatar
Thank you! That is what it is intended for ;). I try to be as descriptive as possible without turning this into something too long for most people to read.
1
SierraHawk
10/04/2014 5:17 am
Level 1 : New Zombie
SierraHawk's Avatar
Btw, These modifiers I would rather termed as 'access modifiers'
1
Cool_Man7
10/04/2014 7:57 am
Level 16 : Journeyman Modder
Cool_Man7's Avatar
Ah, my bad. This was origionally going to include things like final but they were cut short to keep the blog from getting too long :(. Although I don't believe static is an access modifier, more of a class modifier in general. But I see what you mean.
1
SierraHawk
10/04/2014 8:04 am
Level 1 : New Zombie
SierraHawk's Avatar
Oh, please can you ask for a contact on Skype, I'm too a programmer and I like people who are interested in such a matter. Thanks ;)
1
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome