Introduction to Object Oriented Programming in Java

Author
Ben
Published
2012, Nov 20, 11:30 am
Category
Coding
Tags
Java

These are the things which I believe people must know before they can make programs in java. We will go from easy to hard. I assume you have a rudimentary understanding of procedural programing.


Strong Typing

Java is strongly typed. This means you need to declare what type of information or object a variable (aka field) can contain. In a weakly typed language one might declare a variable like this:

n = 0

In Java it is

int n = 0;

That declares a variable of type int (short for integer) and sets the value to zero.

Return Type

In Java, methods (aka functions) need to declare what type of information or object they will return. Methods which don't return anything have the return type void. The type is included before the method name.

Example 1:

int someFunction(int n) {
    return n+1;
}

This method, accepts an integer and stores it in the variable n, which is declared in the parenthesis. It returns an integer, as specified before the name, someFunction.

Example 2:

void anotherFunction(String myStr){
    System.out.println(myStr + ":");
}

This method doesn't return anything. It takes a String object and stores it in the variable myStr, adds a colon, and prints it to the console.

Classes

A class is a division of code. Every class goes in it's own file. The class name is the same as the file name.

Example:

//imports and comments
class SomeClass{
    // All the code would go here
}

Class names, and by extension file names, start with a capital letter by convention. All code goes in classes. I will return to classes later.

Static Methods and Variables

These are procedural methods and variables. These are what you may be used to. We will understand how these are different from instance variables after we learn about objects.

Static variables have the word static in front of the declaration.

Example:

static int i = 43;

Static methods also have static in the declaration.

Example:

static void printJunk() {
    System.out.print("some junk");
}

To call a static method you type class name dot method name.

Example:

double myNum = Math.sin(π/2);

sin() is a static method in the Math class. A double is a 64 bit floating point number.

Objects and Instantiation

This might be a little confusing, so bear with me.

Objects are a way of organizing data. It's a way of allowing multiple instances of the same code to run simultaneously with different values (in their instance variables).

An object is a spot in memory associated with a specific class. An object is called an instance of its class. The class's non-static variables (aka instance variables) are stored in the object's memory. Each instance of a class can have its own values stored in its instance variables. The class's instance methods can only be called with an instance of the class. If that made no sense, I'll show you how it works.

Example:

long t = System.currentTimeMillis();
Timestamp ts = new Timestamp(t);

First Line:
A long is a 64 bit integer.
.currentTimeMillis() returns the time in milliseconds since January 1, 1970, 00:00:00 GMT.

Second Line:
'Timestamp ts' declares a variable of type Timestamp.
'new Timestamp(t)' instantiates a new Timestamp object.
'=' stores the address of the our Timestamp object in the variable ts. If I then said Timestamp ts2 = ts, ts2 and ts would point to the same object.

We can make as many new Timestamp objects as we like. Each one with a different time. The Timestamp class has many useful instance methods which can operate on whatever Timestamp instance we want.

You can read more by Googling the subject.

Instance Variables and Methods

Instance variables and methods can only be accessed through an instance, or from other instance methods in the same class. So an instance method can call static methods but static methods can't call instance methods.

To call an instance method you type object variable dot method.

Example:

ts.toString(); 

That returns the time in as a String.

Summary



Comments


Add a comment