Sunday, January 29, 2017

Learn Java for Beginner easily ! part 13 : Using Multiple Classes

What's up guys ! Hope you're doing well since you have read these tutorials this far cause I'm sure anyone who made this so far has a strong desire to learn Java. So in this tutorial I'm gonna teach you guys about how to use Multiple Classes. By only look at the title I'm pretty sure that you guys already connected a little bit about what we are gonna learn in this tutorial.
So basically If you are working like for a company or you're building a big application or program such as game, software,program, etc. anytime you need to create something bigger, you are gonna have to learn how to use more classes or multiple classes. In Java, everything
starts,excecuted,compiled,read,scanned by the computer from your main class which you type "public static void main" as the method just like a couple of programs that I've given you guys in the couple of tutorials before.

Let me just show you guys how multiple classes works on the pictures below :

1. First Class (this is the main class)



2.Another class (It will be used by the main class)



If you guys don't know how to make a new class. Then I think you should read and learn the
Part 3 first then come back here. This is a reason why there is a tab everytime you guys create a new classes just like we have in the browser. It is because every classes you guys have made will be related each other and it's gonna make your coding activity easier

First I'm gonna start explaining the first program from the Line 5. There you will see a line of text that there is a name of the other class . So Line 5 is what you are going to do in case if you want to call the other class to your main class because you will have to call the other class that you wanna use first before you can use it.The format to call the other class is:

"NameOfClass (space) NameOfClassObject = new (space)NameOfClass( );"

When you typed this line, it means the other class has been called in. Then what are you going to do next ? What are you going to do next is on the next line (Line 6), take a look at it.You will find a text like this :

longbananaObject.simpletext();  // Line 6

I'm going to explain each word on the line of codes above. longbananaObject is the name of the other class + Object (without space), it means that on your main class you are gonna use the longbanana class.

Next is the "simpletext()"  .First I want you to know guys that there could be more than one methods in a class, in this case the name of the method  of the other class that will be excecuted is "simpletext",  you can see it on the Line 4 of the second program , So the simpletext on the code above shows you that this is the methods of the other class that you are going to run from your main class. Every methods has its own codes inside it which contains its function. So depending on the codes inside the "simpletext" method, we all know that this method is to print a text (string).Then we will leave the bracket empty because in this program we don't have any return value.

Here is a simple scheme about how this program being excecuted :

  1. (Line 3) Java run the program inside the main class.
  2. (Line 4 to Line 7) The main class excecuted the program inside the method of your main class. In this class the method is the main method which is will always be the first thing excecuted by Java.
  3.  (Line 5 ) Your main class call in the other class that mentioned.
  4. (Line 6) The other class that has been called is excecuted. Which the program inside it is to print out a text.
So for now I hope you guys learn about this seriously, because if example you are the Head Programmer of a big project in a company. Your task is to tell your programmer team to make the other classes. For example you are gonna tell your programmer team like "Hey you, make me a class that will do this, and you make me a class to solve his class and you make me a class to support her class"  and your task is to collect all the classes that your team have made and combine it into one big program that will do the whole things

So that's it guys for now. Thank you guys for visiting my blog, hope you guys enjoy it and see you on the next tutorial. If you guys got something to be asked, please leave a comment below and thank you very much.

Greetings from Programmer !

No comments:

Post a Comment