Sunday, December 26, 2010

Java Tutorial #15

0
Accessing method (with an argument inside the parameter) into another class

Again, the first thing to do is to create a new subclass.  Of course, that subclass will hold a method that later will be implement to our main class.  In order to understand this topic you must knew already how to make a method and that is recently explained in the previous topic.  So, I recommend everyone to read the previous topic before this one.
This method will hold an argument inside its parameter.  When a method is about to use a variable, the type of that variable must be declared inside the parameter of that method.  That is called an argument. "String " is the variable's data type and "Name" is a variable's name.
Consider this example


In this example, the method called “Method” will be using the variable “Name”.  So, in order to use that variable the method must declare the “Name” data type.  The type and the name of the variable will be placed inside the parameter of the Method and it is called an argument.
“It is an extra data needed by the method in order to work”, bucky said.
That method will simply print the word “Hello” plus the variable “Name”.  The argument specifies that the variable “Name” is a string.
 Back to the main class, import and create a new Scanner and create a new object.  Then, print an instruction to the user.  Use the method, you created from the subclass.


                           

The Output




No Response to "Java Tutorial #15"

Post a Comment