First off, keep in mind that everything is done by Classes. A class is the smallest chunk of a program that can exist by itself. In java, you cannot do anything unless a class is defined to do it.
Things get DONE, by calling "functions" of the class. There are multiple names for this: "functions", "member functions", "methods" and probably a few more I forget. But I'll call them "member functions".
In C, you would have the main() function all by itself. But remember, "class" is the smallest chunk of code that can exist in java. So we have to have the function defined as part of a "hello" class.public class hello { public static void main(String args[]) { System.out.println("Hello world"); } }
is a specially recognised function. In a stand-alone (non-applet) java program, it can be automatically recognized by your runtime as "here's where I start doing things"public static void main(String args[]){}
Note that it has a "static" modifier. The meaning of "static" will be covered later.
NORMALLY, you have to create an "instance" of a class, to do anything. A class by itself is just a template for how an instance of it will act. It's sort of like a mould. An instance is like making something with that mould.
Here's an example of actually doing something with an instance of a class.
"TestClass" is the class name. We make "instance" an instance of TestClass; Note that first you have to declare the type of "instance". Then you actually mould it into something with the "new" operator. If you dont assign it something 'new', anything you try doing with it will be an error, apart from checking if it is null.TestClass instance; instance = new TestClass(); instance.dosomething();
"new" is the only way to actually make a new instance of a class in java. [with a few evil exceptions, which I shall blithely ignore here]
Once you have made the new instance of TestClass, then you can call a
member function from that instance.
dosomething() is assumed to be a member function of class TestClass.
Nope. You want to store information, and maybe manipulate it. For that, you need variables. Just as there are "member functions" that work on instances, there are "member variables" that are specific to each instance. Again, you have to declare an instance of a class to use them.
Below, we have one member variable, and two member functions.
And here's a sample usage of the above class:class simpleclass { int memberv; public void print() { System.out.println("memberv is " + memberv); } public void setmember(int newval) { memberv = newval; } }
simpleclass S1; simpleclass S2; S1 = new simpleclass(); S2 = new simpleclass(); S1.setmember(1); S2.setmember(2); S1.print(); S2.print();
Each instance of simpleclass has its own internal copy of the variable called "memberv". First, we called the "setmember" member function of each instance, to set the variable inside that particular instance of simpleclass. Then, we called the member function that prints out the value of the variable, for a particular instance of simpleclass.
When you call the member function of a class, of an instance of that class, It only changes (or looks at) the local data inside that particular instance. Which is why you call them by instance.member_function(), not by themselves.
I think this is a good place to end the first lesson :-)
Lemme know what questions you have.
[yes, this extends to all you other net.folks out there!]
(Want to go on to lesson 2?)