What is the structure of Java?
As you have heard, Java was designed to be compatible with everything that is possible. Such a restriction forced Java developers to make it so as to simplify the deployment of applications as much as possible, while at the same time providing a logical harmoniousness of the language.
How do classes load?
In order to find a class by name when you call it, a standard class loader exists in Java. It operates on the notion of a classpath. The classpath list is a set of paths where to look for class files. Each classpath can point to both a directory and a so-called jar file (a ziped directory with compiled .classes and a .jar resolution, such as “jA java-archive!”). By default, the classpath includes standard library files and the directory from which you called Java itself. It was in this way that the HelloWorld class was found - java found the HelloWorld.class file and launched the main method in it. Actually, this is how most programs work, even the most complex ones. It all starts with one main'a ...
Class Packages
Package (package) are a set of classes, united by meaning. Packages are usually embedded in each other, forming a hierarchy that makes it clear why. On the file system, such a hierarchy looks like nested directories with sources. So, the sources of package a are in the folder a, the sources of package ab are in the folder a / b, and so on. A typical package path looks like this:
org.apache.commons.collectons
. See, it is immediately clear why it is needed. To use a class in the code of another class, you must import it by writing a string before declaring the class.
import ...;
In addition, if you use the classes of a single package often, you can import the entire package:
import ...*;
This applies to all packages except java.lang - it is imported by default, the System and String classes were taken from it in the last example. In fact, they lie in a certain jar, in the java / lang directory.
')
Well, now you know how the class loader works. In real projects, the number of the classpath is measured in dozens or even hundreds.
Code organization
If you are writing your first little examples and you are too lazy to create a class hierarchy - let it be your right. But remember, in a serious project you will always need to decompose your classes into packages. Usually root packages are created so as to make clear who the author of the code is, and what it refers to.
For example:
ru.vasiapupkin.photomaker
is selected by the root package
ru.vasiapupkin.photomaker.core
here we write the classes responsible for the logic
ru.vasiapupkin.photomaker.visual
here, for example, all of our application windows
and so on.
To create a class
ru.vasiapupkin.photomaker.Starter
you must:
create a file Starter.java in the folder ru / vasiapupkin / photomaker /
register in it the first line (more precisely, to imports)
package ru.vasiapupkin.photomaker;
Collize
“And what will happen if we have two classes with the same name?”, You ask. “Look,” I will answer.
Suppose you decide that you are smarter than Java and have created your own string class - String. But here's the problem, we already have one!
So you have to put your class in the package, say ru.vp.stuff and refer to it like this: ru.vp.stuff.String.
That is why it is not recommended to put classes directly in the root of the classpath - this way you are making your way to incompatibility, because Java requires that each class is defined uniquely. That is why you can not write like this:
import ru.vp.SuperClass;
import ru.mashka.SuperClass;
The compiler will punish you for this because it will not know which one to use.
Moral: choose the right class name and package name.
Chase?
Let's improve the first application. Eh, the classics of the Internet ... Let's create an applet.
Eh, maybe applets ...
import java.applet.Applet ;
import java.awt.Graphics ;
public class HelloWorld extends Applet{
public void paint( Graphics g){
g.drawString( "Hello World" ,15,15);
}
}
* This source code was highlighted with Source Code Highlighter .
So what do we have here? 2 classes were imported, one of them is a standard empty applet, which we will expand. The second is Graphics. Graphics is a concept from the AWT library. By the way, a little digression. AWT (Abstract Window Toolkit) was included in the first Java and was designed for many tasks related mainly to the display.
So, an object of type Graphics allows us to draw on ourselves any kind of string such as lines, lines, circles and so on. In this example, we wrote an indented line.
The paint method is not written here for the balda - it overlaps the analogous method of the Applet class, and when java redraws this particular applet, it will call this method.
It is quite simple to look at our applet - we write small HTML:
< body >
< applet code ="HelloWorld.class" codebase ="file:///home/devgru/" width ="150" height ="30" ></ applet >
</ body >
* This source code was highlighted with Source Code Highlighter .
... or maybe by application ...
Let's try to do HelloWorld in a standalone application.
import java.awt.* ;
import javax.swing.* ;
public class HelloWorld extends JFrame{
public static void main( String [] args){
new HelloWorld();
}
{
add( new JLabel( "Hello world" ));
setSize(200,200);
setVisible( true );
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
* This source code was highlighted with Source Code Highlighter .
Here we completely import the main classes of the Swing and AWT packages. Swing is later than the AWT library, now it provides the display of the main part of the graphical interface for Java applications.
So, in main, we simply create an instance of the HelloWorld class.
Now our class is inherited from the JFrame class. This is a class from Swing, it is a window that is displayed until it is closed.
The {...} block is a “common constructor”. It is added to any constructor of our class. Since we have none, it is added to an empty constructor without parameters, which is created on the fly if the class has none.
We add a new object of the JLabel type (i.e., an inscription) to the window, then set the window dimensions and display it. The last line is needed for the application to finish when the window is closed. Thus, you can be sure that after the window is closed, your application will not remain in your memory.
It should be run in the same way as the past: we write, compile, run.
And maybe servlets? Probably later.
In these two articles, I tried to give you an initial idea of ​​the possibilities of Java in general. Outside today's article (probably will be in tomorrow's) remained servlets and other joys of the server part such as JSP-pages, as well as MIDlets - applications for mobile phones. I could consider both this and that, but I would like to know what the readers want more. In addition, you may need to talk about the very basics of the language. At about the same level of detail as the beginning of this article. Write in the comments which article you would like to see next time:
- classes and interfaces: OOP in Java;
- letters-numbers-lines: work with basic types;
- creation of window applications using Swing;
- from small to large: servlets, midlets and 2 words about portlets.
When you unsubscribe, it will become clear where to dig further. Thank you all for your attention.
Link for those who are too lazy to wait for tomorrow: the
basics of the language (eng.) .