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.
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 |
3097333
6
Create an account or sign in to comment.
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.
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.