Well, a few days back i started this tutorial series on the Java programming language. Now i feel it’s the time to move on. I realy have no clue if the series will be of any help for any of you but regardless i still feel like going on.
On the last post i illustrated how to write the first "Hello world" program. Although it’s a very simple program it has many points that need to be clarified. Let’s use a bit of a modified "Hello world" to make things alot clear. So here it goes…
public class Foo {
private int number;
public static void main(String args[]){
Foo f = new Foo();
f.printMessage();
}
private void printMessage(){
System.out.println("Hello instance world! Number: " + this.number);
}
}
Now, here is a big difference from the previous example. We create what we call an instance of the Foo class and then call the method printMessage() on the instance. We have explained what an instance of a class is but we have to see what it exactly is for Java.
Let’s explain what we do on line 4. We actually define a new variable called "f", which has a type of "Foo". Then we create a new instance of the class "Foo" and assign it to this variable. Then on line 5 we use this reference for the instance (if we were using C we could say it is a pointer) to call the method "printMessage". As you can see, the "f" variable is "stored" on one location of the memory pointing on the instance which is on another memory "block" holding all the instance data. Now when a new instance is created all local variables ("number" here) are initialized. Here is how they are initialized:
- Primitive number types (int, float etc) are initialized to "0".
- Primitive boolean is initialized to "false".
- References are initialized to "null".
Now, there are a few words up there that need to be explained, "public" and "private". These are related to what we call the scope.
Scope: The range within a variable, class or method can be referenced.
There are these primary scopes in Java for methods/classes/variables:
- public: Visible to any other class or package.
- protected: Visible to all classes that inherit this class and to all classes within the same package.
- default (no keyword is mentioned): Visible only to all classes within the package.
- private: Visible only to the current class.
Do not worry about what "inheritance" and "package" means. We will explain these later. Now that we explained these, can you imagine the output of the above program? Here is what will happen in detail:
- The program will start from the main method.
- It will allocate space for the "f" variable that is type "Foo".
- It will create a new instance of "Foo" and assign the reference to it to "f".
- The call to the printMessage will come.
- printMessage will be called and print out "Hello instance world. Number 0". The "number" will be "0" because it will be initialized upon creation of the instance.
- printMessage will return.
- main will return and the program will terminate.
There is one keyword up there called "this". Don’t worry much about this one. We will digg on it on a next part of the turorial series that will explain better instances. I know this left many things back but don’t forget this is just part 2 and it covers the very basic. Be patient, everything will be covered slowly. For now tell me what you think about this part and post with any problems!
Once again thx for this 😉
I referred my friend to this 😀
Thx for commenting and hope it is useful to anybody out there…
Thank you for this and other tutorials. They are very nice to get a decent introduction to Java. Keep it up!!