Being objective

So far, we have learned about Variables, Primitive types, and before that, the general concept of objects. This lesson teaches how to intermix all of the above.

It is common to give each java object definition its own file, but it isnt strictly required. This example expands on the my_object type previously, to call another object, to first store, and then print out, a number. It shows you how to make your own object methods.
There's a bit of a trick about these particular methods: they are "static" methods. However, the significance of this, will be explained in another lesson, so you can ignore that bit for now.

Multi-object use

class that_object {
	static int stored_int;
	public static void save_int(int i){
		stored_int = i;
	}
	public static int get_int(){
		return (stored_int);
	}
}
class my_object {
	public static void main(String arguments[]){
		that_object.save_int(3);
		
		System.out.println("The number that was stored, is: "+
			that_object.get_int() );
	}
}

// As with prior examples, if you copy this code into a file named
// my_object.java and then compile it, it should actually be runnable!
There are a lot of concepts introduced in the above example.

First, you will of course notice that we no longer have just one object, but two. The main object is, unsurprisingly enough, the one with the "main" method in it. The purpose of the "main" method, is to tell the computer what to do first, when you start a program up.

Next, you may notice that "that_object" has a variable called "stored_int", outside of any method. This is the fundamental usage of most classes: To store data inside them. You might consider that, just as variables are storage containers for data, objects are storage containers for variables.

In many ways, objects are just super-hero variables. The big difference between objects, and regular variables, is that you can restrict the way that things outside the object, interact with the variables inside the objects. Java style classes are not the only way to do this. It is actually possible to use "Object Oriented Programming" with C code". But since this is a java tutorial, I won't go further into that here.

Another thing you may note, is that our System.out.println() call, got split across two lines this time.
Yup, thats perfectly allowable. Java treats everything as one line, until it hits a semicolon.

Prev Lesson: Primative types --