First time here? Please, start at the beginning.

Monday, August 23

Part 1.2: A few (nay, lot of) words on Java syntax and packages

I said I was going to post an example, but I decided I needed to cover this right quick first.

Syntax of a typical file
Not strictly syntax but still important is the usual layout of a Java source code file:
>import statements<

>public class, enum, or interface definition<

>optional non-public class, enum, and interface definitions<
That last part is important, and not necessarily obvious to someone new to Java. I won't go into detail on the hows and whys, just know that a public class must be declared in a file wit the same name, and a non-public class can generally only be used by other classes in the same source file. THIS IS NOT A BEST PRACTICE. But it is useful for a quick "hack" program like uChat.

Wondering what enums and interfaces are? I'll explain interfaces later, but enums are out for now. They can be useful but for now I don't plan to cover them here. import statements will be described below.

Syntax of a typical class (includes methods and variables)
modifiers are not exhaustive, only the common ones are listed
[>access modifier, usually "public"<] [abstract] class >name< [extends >superclass<] [implements >interface<[, >more interfaces<] {

/* variable definitions */
>access modifier< [static] >data type< >name< [= >value<] [, >name2< [= >value2<];

/* method definitions */
>access modifier< [static] >return type, must be a data type or "void"< >name<[ ]([>data type< >name<[, >data type< >name<]) { >body< }

/* method required if class is to be run as a program */
public static void main(String[] >name, usually "args", but can be anything<) { >body< }

/* constructor definitions */
// constructors are used to create objects, they set up the object's internal state
// they look like a method, except they canNOT be static and have no return type
>access modifier, usually "public"< >name, must be the same as the class<[ ]([>parameter list, same as a method<] { >body< }

} // end of class
I hope all that makes sense. [this is optional] >this should be replaced with something appropriate<


Packages
If classes are templates for objects, then packages are books of templates. Packages are used to group classes together; generally classes that are used together for a given task will be in the same package.

For example the java.awt package contains the Abstract Windowing Toolkit, which can be used for creating GUIs. Similarly, the java.awt.event package contains classes for handling events "fired" by a user interacting with GUI elements (a button press, or a mouse movement for example).

The java.awt.event package is also an example of a package contained in another package. Packages are a bit like folders (or directories) on your computer. Some of the standard packages in the Java Library:
  • java -- the "standard" Java libraries, contains only other packages
  • java.awt -- the Abstract Windowing Toolkit, older library for designing GUIs and what uChat uses
  • java.lang -- automatically imported for every source file, contains the System class among many others
  • java.io -- classes for file I/O and filesystem access
  • java.net -- classes for network operations: sending and receiving via TCP and UDP, managing network addresses (DNS names, IPv4 and IPv6), depends on several classes in java.io
  • java.util -- a number of useful classes, includes the Collections Framework (lists, stacks, etc.) and the Random class
  • javax -- the "expansion" Java libraries; no, I'm not entirely sure what that means
  • javax.swing -- Swing, alternate (and preferred) GUI toolkit, duplicates and expands on the functionality of java.awt
Finally importing
import statements allow you use a class in a package with its short name rather than its long one. For example, in the java.awt package is the Button class; its full, long name is java.awt.Button. However if you import it, you can call it simply Button. import statements come in 2 varieties: single class and entire package.

Example:
import java.awt.Button;
import java.util.*;

The first imports java.awt.Button but not java.awt.TextArea. The second imports everything in java.util. It's generally easier to import an entire package. I know of no advantage to importing single classes.

Okay, next time: Example.
Then, we'll get started on uChat.

No comments:

Post a Comment