Logo

Home Basics Structures Collections Constructors and Methods Classes

Classes:

Now to put it all together. All the components on the other pages are all key components to a class in Java. Some applications have more than one class. Below is a flash video I made of a code I wrote in one of my classes. The class has to have the name of the class, fields, constructor, and methods. Most Classes have a main method the code below does not have a main because it was made for a program called bluejay. Any more I use eclipse and with that I have to have a main method to run the program. There are a lot of classes out there that you can use the help your classes you make you just have to import them from the java library.

The layout of a site is as followed. You use a lot of semicolons, paranthesis, and curly brackets in the code. Missing one of them can mess up the code you are writing so that it does not work properly. Names of classes are always capitalized but names of fields only the first word is not capitalized. An example would be a class called ThisIsAClass vs a method thisIsAMethod.

Part
Example
Field(s)
public class Calculator -initiates a class called Calculator.
{
private int accumulator; -field or global variable
Constructor(s)
public Calculator() -notice same name as class
{
// initialise instance variables
accumulator = 0; -initializes fields.
}
Method(s)
public void add(int number) -takes a parameter and is a mutator method
{
int amount = accumulator; -local variable
accumulator = amount + number;
System.out.println ( amount + " + " + number + " = " + accumulator ); -prints to screen
}