Updated 2011-05-02 21:02


This page helps you quickly learn the fundamentals of the Java programming language.


These are a few of the basic building blocks of any Java program.


Programmers write programming code (usally shortened to "code") to accomplish tasks. Code is almost always put inside of "methods". A method might be named "calculateTax" or "saveToDatabase". In object-oriented languages like Java, methods are almost always put inside of classes. A class might be named "Invoice" or "Person".


CodeThe text programmers create using a programming language
MethodContains code (roughly synonymous with "function")
VariableContains a piece of data (or an object)
ClassContains methods and variables (methods can also have their own variables)
Object (long definition)Exists during program execution and contains variables having actual data (as defined by a Class)
Object (short definition)An instance of a class


When a program runs, the classes are used to create objects. Objects might be named "currentInvoice" or "lastPerson". Methods are usually invoked on objects to make things happen, such as currentInvoice.calculateTax() or currentInvoice.setQuantity(200).

Tying them together

Question Answer
Hierarchy of how code is organizedCode (exists in) Methods (which exist in) Classes
What do you need to create an object?a class
What do you usually need to invoke a method?an object
What is the point of a class?to create objects
Where does code exist?in methods
Where do variables exist?in classes or methods


Here are some corresponding code examples.


Question Answer
A comment (does nothing)// Some Descriptive Text
A line of code that displays a stringSystem.out.println("Hello");
How to define a methodvoid sayHello() {
  // Code goes here
How to define a variableString price = "$100.00";
How to define a classclass Invoice {
  // Variables go here
  // Methods go here
How to create an objectInvoice currentInvoice = new Invoice();
Invoke a method on an objectcurrentInvoice.calculateTax();

Seeing it all together

Here's what a simple Java program might look like:


// You need a class to put your methods into
class Invoice {


    // You need a method to put your code into
    void calculateTax() {


        // Here's some code
        System.out.println("There's no tax for now.");


// You need an object to call your method
Invoice currentInvoice = new Invoice();


// Finally... you can invoke your method to run your code

Identify the pieces

In the above code, identify the pieces:


ThingExample in above code
variablecurrentInvoice (it's technically a variable named currentInvoice that references an object of type Invoice)

Recent badges