Sunday, December 26, 2010

Java Tutorial #14

0
Accessing a method (with empty parameter) into another class
                                 

 First thing to do is to create a new class (sub-class. If you already have your main class) 
You can do this by clicking the new class icon in the toolbar then choose "Class".
In the Dialog Box, uncheck the check box with the option of "public void static main(..)" then Finish.


In the SubClass.

Then create a new method.  A Method is a function inside your program.  It is like a little sub-program inside your main-program.  You can type actions and bunch of codes inside this method.



public means that all of the class can use this method. This can be replace by private which means that only that class can use the method. But, in this case, we don’t need to do that.
void means that the method will gonna do something but it will not return any data at all. This can be replace by any data types you want to associate with your method.  e.g. String if the method will return a string value.  int if the method will return an integer value.
MethodName( ) is any name you want to associate with your method. In this case, we will not put a parameter inside that method.


In the Main Class.


After creating your method, go back to your main class.  Then connect your main class to your sub-class. This can be done by creating an object that will hold the data from your subclass to the main class. Here’s how:

First indicate the name of your subclass. 
Create an Object. You can assign any name for your Object Name. 
Put an equal sign and type the word “new”, the name of your subclass and put an empty bracket.


Then you can now use the method declared. By using the Object you previously declared and the name of the method you created from the subclass.


After that it will output this one. It will do the command you created from the subclass.







No Response to "Java Tutorial #14"

Post a Comment