|
Lesson FourBlocks and overloading | |
The coding for any method or class that is between "{" and "}" is called a block. Think of a block of code as children's building blocks. No matter how hard you try, you cannot make the two blocks equal one. You can make them equal one shape (or in our case, one class or method), but never one block. For example: has two blocks within the program. The first block is: and the second block is: This second block is called a nested block because it is inside the first block. The scope, or area you can reference in, of this block is called local scope, meaning only this block can use the included data. Anything outside of this block would be considered global, meaning that any method or block after the declaration of the global variable can use that variable. You can use nested blocks to write programs inside of programs, or programs that bypass other programs data. This is called overloading. Here is a form of overloading a method: There are other more advanced methods you can use to create desired outputs. This next program involves using the Math class. Another advanced method is the import java.util.*. This whole class, which is included in the JDK when you download the program to your computer, will import a library of classes and methods for you to use without having to write out every method you wish to use. In future lessons, you will learn more about other class packages that are included with the Java JDK. These packages make it so you don't have to "reinvent the wheel". The following is an example of this class: You cannot type import java.* to use all the packages. You have to access each package individually. Although the word "import" is at the front of the call, "import" simply tells the program that you will be referring to some of the data and method names that are part of the class you are importing. Below is a program that computes three commission rates. The first method computes the sales and the double rate. The second method computes the sales and the integer rate by converting the integer to a double. The third method does the same as the second, only it contains its own rate. |
|
Question: I came to this site to learn Java. I don't want to learn some programming language. When are you going to teach me how to do things relating to HTML? Answer: On the contrary, you ARE learning Java because Java IS a programming language. As far as HTML goes, you'll be learning how to incorporate Java with HTML before you know it. | |
If you have a questions about any of the lessons, feel free to ask. ![]() |