"Hello, my young friend. I hope you haven't forgotten that I'm a 16th generation bureaucrat. If I hadn't systematized all my knowledge, I would never have achieved what I have. I'm full of useful information. I'm going to help you with some tasks. For starters, let me tell you about a typical Java program."
"I'm listening."
"Fact number one. A Java program consists of classes. Each class is stored in an individual file, whose name coincides with the class name. The file extension is java."
"So, a program consists of a series of files with the 'java' file extension, and each file contains code for just one class, right?"
"Absolutely correct, Amigo. If a file is called MyCat.java, it contains the MyCat class."
"Fact number two. When we have many class files, we group them into folders and subfolders. Additionally, classes are grouped into packages and sub-packages. The names of packages and sub-packages must be indicated in class code, and they must be identical to the folder and subfolder names on the drive."
"Thus, on the one hand, we have files stored in folders, and on the other – classes stored in packages. A class name must also coincide with the name of the file describing the class. The package name coincides with the name of the folder where the class is stored."
"Can you give me any more details?"
"The names of the nested packages are divided by periods, almost like URLs."
"In other words, suppose you have a class named Cat, stored in an animals.pets package. That means:
The hard drive has some folder (we'll call it src) where all the project files are stored.
It contains a folder named animals, which in turn contains a pets subfolder.
The pets folder contains a Cat.java file, which in turn contains code for the Cat class."
"I'm not quite sure I understand."
"Look. The structure of the classes and packages mirrors the structure of folders and files on the drive. If we have a file named House.java, stored in the src/com/houses folder, then there is a class named House, stored in the com.houses package."
"Got it."
"You seem to be picking this up rather quickly. Look at the screen. This is code for a small class. I've labeled all the key parts:"
PACKAGE NAME
package com.futujava.lesson2;
import java.io.IOException;
/**
* User: General
* Date: 12/21/12
* Time: 11:59
*/
CLASS NAME
public class Task1
{
⎤
private static String TEXT = "Kiss my metal rear actuator";⎥ CLASS VARIABLE
⎦
⎤
public static void main(String[] args) throws IOException ⎥
{ ⎥
SCREEN OUTPUT SINGLE-LINE COMMENT ⎥
System.out.println(TEXT); //Display a single string ⎥
MULTILINE COMMENT ⎥
/* ⎥
This is a multiline comment. ⎥
The code below will display three identical strings. ⎥ main() METHOD
*/ ⎥
VARIABLE DECLARATION ⎥
String s = "Ho-ho-ho!"; ⎥
METHOD CALL ⎥
printTextMoreTimes(s, 3); ⎥
} ⎥
⎦
METHOD PARAMETERS ⎤
public static void printTextMoreTimes(String s, int count) ⎥ METHOD SIGNATURE
⎦
⎤
{ ⎥
LOOP ⎥
for (int i = 0; i < count; i++) ⎥
LOOP BODY ⎥ METHOD BODY/CODE
{ ⎥
System.out.println(s); ⎥
} ⎥
} ⎥
⎦
}
"Heh, it's as clear as can be after just one explanation."
"Good! That's all we need. Just try to understand at least something. A complete understanding will come with time. Now, I'm going to catch some Z's. Somebody else will continue your training."
GO TO FULL VERSION