[MUSIC]
>> Greetings, everyone, and welcome back to
Microsoft's Java for Beginners series.
This is Part 2 of methods.
In Part 1, I described some of
the concepts that you need to know to work with methods.
In Part 2, I'm going to show you some examples in action.
In Part 1, I showed you
a simple main method and it
just runs a little loop that returns a value.
In example 2,
I showed you how you actually create
a separate method that is
called by the name method and
returns in this case the same values.
We covered parameters and variables a little bit there and I
talked a little bit about instance variables and local variables.
Now when I work with example 3 here in part 2,
I'm going to show you a pretty good example
of calling methods with arguments and parameters.
There's a few things you need to know about variables,
arguments, parameters,
classes, class instances, and local variables.
Let's get started with that. The first thing
to remember is passing and managing variables.
Java is always pass by value.
What that means is that you cannot pass values directly.
Some of you might have used other programming languages or you can
just instantiate a variable,
pass it to another function or class and
have that value preserved as it gets passed around.
That's not how it works in Java.
I'll explain a little bit about
why with memory management and a few other things.
It also has to do with
object orientation and classes of methods in general.
But there are a few little steps
that you might have to go through with Java that you
might not have had to go through with
some simpler programming languages.
What you use for
actually managing variables are
these things called getters and setters,
and what getters and setters do in
Java is represent encapsulation.
Encapsulation is where you're actually accessing
values that are created in
different places and can be passed around with an object.
That means that when you have an instance variable, for example,
the instance variable itself is
created in the class but not in the method.
You actually have to pass a value
from an instance variable to a method.
When you pass it to a method,
it becomes a local variable.
Local variables can be created inside
methods and can be just managed inside methods,
but they only exist inside that method.
There's two ways of getting values into
your methods to be able to use them in your application.
That's by using getters and setters to pass values.
You pass those as parameters that become local variables or just
simply create a local variable inside the method
itself if you're only using it for that method.
Objects, classes,
and related instance variables are stored in the heap.
Java uses space in the heap for stack management,
but you don't have access to it directly.
When you create a new class in Java,
it actually creates memory in the heap for that class,
and then any methods that are running go on to the stack,
and they get loaded on the stack and used and as soon
as they're idle or unused for a while,
garbage collection comes along and cleans
them out so there's space in the stack
for the next set of variables and methods that you need to use.
Because of this, objects in the stack
are never explicitly deallocated.
Local variables are created in stacks and
sometimes are referred to as stack variables but that's rare.
Local variables like primitives and
object references are created on stack memory,
objects are created on heap memory.
The object itself, if you remember a method can
run and it can create different things
and function by itself or it can be created inside a class.
I'm not doing a very good job of that either, although.
In Part 1, I described how you can create
methods and then you instantiate
objects from classes using constructors.
The constructors actually put
that class onto a heap because it's an object.
The method itself runs on a stack and stacks actually
are managed and created
for method invocation and for local variables and primitives.
Instance variables is important to remember are part of
heap memory because they are part of
the object, they're part of the class.
They're instantiated as a class.
They always have a default value.
Local variables are part of the stack and can
be added and removed as needed for your application.
Last but not least,
a little bit about modifier.
When you mark a method as public and static,
it actually makes it behave like a global variable
because it's accessible in any class of application.
If you want to mark something as a globally available constant,
you mark it as public static and final.
Final means load this once,
it's a static value
that's going to be used and it's not going to be changed.
The JVM has confidence that it can load once,
it's never going to be asked to change
that variable while that thread is running.
That's a little bit about stacks,
heaps, objects, and variables.
I hope that cleared a lot of things up.
It's really good to know this stuff as you go through working
with Java and a little bit about marking
things when you're working with methods in modifiers to
make sure that whatever you've built
is accessible by whatever is intended to be used.
Let's have a look at the code. In example 3 here,
we've got our main method.
It calls a couple of things here.
If you recall from the other examples I showed you,
this is calling something called shareLoop and
it's creating a new shareLoop using a constructor.
We've got a shareLoop named s1 and another one
named s2 and these are
objects that are going to be created using these constructors.
Then we go and get some instance variables.
The instance variables themselves,
we do something called setSharey and then we have
an assignment here that calls shareLoop using s1.
If you recall, I mentioned that you can
pass method values as
parameters and that's what we're doing right here.
This is actually passing a method as a parameter inside of
a call to another Java class or another Java method.
Then s2 setSharey goes and get sharey and sharez.
If you see here, where is the share loop code?
Well, it's in actually a different file.
Here's class shareLoop.
This is called by example 3 when we create these constructors.
Private int sharey is an instance variable.
It's private to the class.
Private static into sharez,
we instantiated at zero because we're going to start with zero in
our loop and private static
equals private to the class and all instances.
If you just have static without private,
it's initialized when the class is loaded and it's
private in the class in all instances as well.
But private static means it's private when you start here.
Public static, final string.
This is just a string that we're using.
A final is a constant meaning this value here will not change.
When the JVM runs,
it does certain things depending on
the keywords you use when you're instantiating these.
Here's your getters and setters.
I showed in example 3 here,
we have a setSharey and if we look down here,
we have the getSharey,
getSharey is passing a method as a parameter.
I mentioned you could do that in Part 1.
When you pass a method as a parameter,
it passes it as the value.
In this case,
it'll say getSharey is
going to go out and it's going to get whatever
the value is that we set in setSharey.
Sharey equals y. Now,
this is what you have to do to create a variable and a class.
Because you're creating several different objects
from a single class,
they all have to be able to access this variable independently.
That's why you have to do the getters
and setters when you're passing these.
Passing things as a parameter is actually going back into
the class and manipulating
these values for you as part of the class,
not here in the calling application or the calling method.
It's actually setting them
inside the class because that's what
you have to do with instance variables,
set them inside the class.
Then we've got a value here which is just a local variable
and it's public int and it's just
going to return a value here that's only
used in this loop as we pass the variable around.
You can see here, these actually are
the getters and setters that manage the local thing.
If you try to use something simple like this,
I just put this in commas to show you.
If you just want to say int a equals 0,
int b equals 0,
and then call the share loop from here,
it's just not going to work because these values need to
be contained inside encapsulation.
They need to be encapsulated inside
the classes being called rather
than the class that is doing the calling.
Then the next thing you do is you could just return a value here,
that public final constant that
we declared over here when it says, "Loop is done."
Public static final string
f is public and it's static and is final
so this application could just simply access from the object s2,
they can access the f variable and return it.
Here's what it looks like when it runs.
It just says instance variable
now passed creating before the loop.
Then we have the local variable that just
does the looping for us and then it
returns "Loop is done" at
the end to show you that everything was done there.
I actually ran it twice.
I think I clicked it twice.
That were some examples of working with methods.
Please check out the rest of
the topics in Microsoft's Java for Beginner Series.
[MUSIC]