Sun Java Tutorial
Sun Java applications apply to a wide variety of contexts. Java programs built using the object oriented model are composed of distinct parts functioning together as a whole. Designing a Java program involves dividing up the various tasks required among a set of application parts. At a basic level, building a Java application in this way involves creating classes. Classes are specialized sections of code dedicated to one or more related tasks and can be called on from elsewhere to perform these tasks.
Things You'll Need
- Java Runtime Environment (JRE) installed on your computer
- Integrated Development Environment (IDE) such as Eclipse or NetBeans
Instructions
-
-
1
Create a project for your program. In your IDE, create a new Java project and name it "TutorialProject" or something similar. In your project, create a new class, and name it "MyMainClass." Open the new class if your IDE has not opened it automatically. Enter the following code:
public class MyMainClass {
public static void main(String[] args)
{
//main code goes here
}
}
Your IDE may have filled part of the code in for you. This is where you will enter the code that is executed first when the program is run.
-
2
Create another class. The main class typically utilizes other resources within the Java project that you create or import. Most applications use multiple classes, each of which have their own specific role within the project as a whole. Create a second class in your project, naming it "MyHelper." Open the new class and enter the following code, again, your IDE may have filled some of it in for you:
public class MyHelper {
//class declaration code goes here
}
The class declaration is a definition of what an object of this class will do. Don't worry if these concepts are new to you, as they will start to make sense when you use them in your programs. The only way to learn programming is by doing it.
-
-
3
Give your new class a constructor method. Once you have completed a class declaration in your project, you will be able to create objects of that class elsewhere in the application. When this happens, the class constructor method is called, so you need to define what it will do. Typically, a class has one or more items of data within it. These may be represented as "Instance Variables," meaning that they exist for every instance of the class, i.e. every object of the class that is created. Enter the constructor method within your "MyHelper" class declaration:
public class MyHelper {
//class declaration code goes here
//instance variables for the class
private String myName;
//constructor method
public MyHelper(String name)
{
myName=name;
}
}
The class is a simple example containing just a single instance variable.
-
4
Instantiate your class. Go back to your main project class and enter code within the main method as follows:
public static void main(String[] args)
{
//main code goes here
MyHelper mainHelper = new MyHelper("Sue");
}
Here you are creating an object or instance of the "MyHelper" class you defined. The constructor method in the class requires that a parameter of String type be passed to it, which is why the "Sue" example text string is included within the parentheses. Using "MyHelper()" is shorthand for calling the constructor method of the class. The constructor carries out whatever code is contained within it and returns an object of the class to wherever it was called from.
-
5
Give your class a method. At the moment the "MyHelper" class is not very helpful, as it doesn't do much. Go back into the "MyHelper" class declaration and add the following after the constructor method:
public void writeName()
{
System.out.println(myName);
}
Now go back to your main class and add this line after the line creating the "MyHelper" object:
mainHelper.writeName();
Save your files, compile and run the program. All that should happen is the string "Sue" being written out to the standard output console in your IDE. This simple example demonstrates the way in which classes and objects are used to carry out tasks in a Java application.
-
1
Tips & Warnings
Take time to design your Java programs from an abstract level before you start writing code. Even if this only involves scribbling down objects on a piece of paper, it can save you huge amounts of time and stress.
Try not to go against the conventions in Java for naming classes, methods, projects and packages if you're using them. They may seem arbitrary, but they make code easier to understand and can help you to keep track of what's going on.
References
Resources
- Photo Credit red coffee cup image by jovica antoski from Fotolia.com