Java Fundamentals

Methods

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

Check out a free preview of the full Java Fundamentals course

The "Methods" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie discusses a collection of statements that perform a task, known as a method. Parts of the method, including the access modifier, non-access modifier, return type, method name, parameter list, and method body are also discussed in this segment.

Preview
Close

Transcript from the "Methods" Lesson

[00:00:00]
>> So methods are things that break complex programs into small manageable pieces. So we've already been making calls to some methods, right? We call system.out.println when we want to print something. Println is a method. We call scanners next methods when we want to input something. That's a method, right?

[00:00:26]
So methods are designed to be used by other code. Here's an example of a method declaration, okay. This very first line of the method is a header. It's called the method header. So I'll walk you through like each piece of this method. So the very first part is an access modifier.

[00:00:51]
This could be public, it could be protected, could be private. This indicates who has access to this method outside of the class, all right. So, within the same class, anyone can access the method, but then outside of the class you can then specify what other code is able to call this method.

[00:01:17]
Public means any method anywhere, any class can utilize this method. We'll talk about private and protected in a bit. The next word here is a non access modifier so valid ones are static you can use final, you can use abstract, you can use synchronized. We'll get into what each of those mean.

[00:01:49]
For now, we've been using static, okay? The access modifier, and the non-access modifier, are both optional. You don't need either one of these. If you did not put public, like you didn't put anything, it would default to protected, which is a package level access. Meaning any other classes in the same package can use this method but not outside of that.

[00:02:19]
The next piece of this is the return type. So, all methods are required to specify a return type. This means what is the data type of the value that you will return back to whoever called you. If you are not gonna return anything because some methods, they just want to do some execution but not necessarily returning anything back.

[00:02:47]
If you still have to specify a return type it would just be void. The next part of the method header is the name of the method. So the name of the method by convention starts with a lowercase letter. If you use camel case and it's typically in the form of a verb because methods do something righ?

[00:03:13]
So for me to call a method you want a name that represents what it is that method does so I know to call that one, right? This isn't always the case that's also convention. Sometimes that just doesn't make sense and that's okay as well. But in this case we have calculate sum.

[00:03:35]
The exception here is also for methods that return a boolean value. So usually those you're asking some sort of question. So those methods names reflect a question like, is number positive or something like that. So boolean methods typically start with like is or does or something like that.

[00:03:58]
The next part of the method header is the parameter list. Symbol which is enclosed inside of a set of parentheses, the parameter list is required for methods. Even if there are no parameters, you would have an empty set of parentheses. What this parameter list indicates is that this is the data that you have to send to me the method if you call me.

[00:04:27]
If you call me, I need some data. So for example, calculate some is saying, well, I need two numbers to calculate, otherwise I can't help you, right? And you specify the data type of the parameter you need as well as the name that you are calling it. This name, like number 1 and number 2, is local and specific to this method, it means nothing to anyone other than some sort of hint about what type of data are you looking for here?

[00:05:00]
So when you call me and I say, give me two numbers, number 1 and number 2, you have a good indication. Okay, I get what they want here, all right. So methods are uniquely identified within a class by their signatures. And the signature of a method is the name of the method coupled with the parameter list.

[00:05:25]
And by parameter lists, the names of the variables are really irrelevant is the data types in the order in which they appear. So if I were to say what was the signature of this method, it would be calculate some int, int, okay? Now, inside of the curly braces of the method is the method's body.

[00:05:49]
This is all of the code that would be executed if you call this method. So here we're going to, add number 1 to number 2. We're gonna store that into a variable call sum. And then the method if it did specify a return type other than void, the last line of the method must return the value.

[00:06:14]
And it has to be of the type that you say you were gonna return back because whoever called you was expecting that right? So like when we call scanner.next double, we are expecting a double to be returned back to us, right? Questions on method definition.
>> Just one.

[00:06:34]
>> Yeah.
>> I assume like that's static keyword, were earlier. The very start, you mentioned Java is like statically typed. Does that have anything to do with like that?
>> No, so this static is more like you don't, and I'll talk about it more after we talk about objects, but you don't have to instantiate an object instead of state and everything in order to use them.

[00:06:56]
You can just use the method without that, this way it means all right. So I guess if I were to think about it, I don't need to do like anything dynamic in order to use this. It's a static kind of flat, use it whenever you want to kind of method

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now