Before i dig deeper into Java, i need to explain a few more things that i think have been underestimated before or have not been metnioned at all. There are many key concepts that i am surely missing in this series but this is why it’s called a “crash course”. I am trying the best i can to explain the most of it but please forgive me if i leave anything out. So let’s get into the point.
1. Variable initialization
A very important thing to grasp is that Java initializes all the variables (members if you prefer) of a class. Booleans are initialized to false, numbers to 0 and any other reference to null. Please notice here that even a String is initialized to null since it is a reference to the class String and not a primitive. Now, all the variables that are defined with a limited scope, such as variables defined within a method or a code block (whiles, fors, ifs etc) are not initialized. But, if you try to compile without initializing a limited scope and try to use it the compiler will pop up an error at the point of the usage saying “variable variable_name might not have been initialized!”. For instance look at the code below:
private void test(){
String foo;
System.out.println(foo);//the error will appear here!
}
If you try to put this method in a class and compile the compiler will throw an error on the printing line, where we try to use the uninitialized variable “foo”. This happens both for the primitive types (booleans and numbers) and references to instances.
2. The static variables and methods
These are one of the most interesting variables. We already know that to be able to access a class member or method we need an instance of the class first. Well, if we declare a method or a variable as “static” we say that we don’t need the class instance. A good example is an instance counter. Consider the following example:
public class Foo {
public static int count;
public static void main String(args){
new Foo();
new Foo();
(new Foo()).print();
}
public Foo(){
count++;
}
private void print(){
System.out.println("Instance count is: " + count);
}
}
Now can you imagine what the above code will do? Well, it will make three instances of Foo that’s for sure. But when we print out “count” what is the value that we will get? Well, the variable count is a static one so you can think is as a cross-instance one. This means that all instances of the class will share the same variable. The first instance increments the variable to 1, the second one sees that value and raises it to 2 and, finally, the third one, raises it to 3 and prints it out. So, the value that will be printed out is 3. Now, if you wanted to see the variable from another class, since it’s a public one, you could do something like “Foo.count”. Now, same thing happens with a static method. If you characterize it as “static” any class can reference it. See the example below:
public class Foo
private int num;
public static test(){
System.out.println("Hello static method world!");
}
public static test2(){
System.out.println("The num is " + num);//error here!
}
}
Now, if you want to call the “test” method from any class (and i repeat since it’s public you can, if it was private you wouldn’t be able) all you have to do is say something like “Foo.test()”. Now, before that, if you try to compile the above example you will get an error at the line specified saying “Non static variable num referenced from a static context”. This is a serious problem since, as we said, to access test2 you don’t need an instance of Foo, but the variable num is a member variable to the class which is non static, meaning that it will be initialized (even worse the space will be allocated) upon the instance creation. So, trying to use a non-static variable and method from a static one is all the way wrong.
I would like to point out one more thing we have been typing all this time. The “main” method is a static one. This is very logical, since the program has to start at some point and at the beginning there is no instance of no class what so ever.
3. The final variables
Now, this is the final thing for this tutorial i would like to point out. When we say a variable is “final” we mean that since we allocate it and assign a value, this value cannot be changed from nowhere and under any circumstance in the program. We usually do that with a static variable that we want to have a certain significance or definition. For instance, if we want to define that the error color is the color red then we would do something like this:
public class Foo {
public static final String ERROR_COLOR = "red";
}
Now, any class can “ask” Foo for the error color like this “Foo.ERROR_COLOR”. This is a common thing for definitions.
With these ones, i think i covered enough things. If you have any problems, suggestions, corrections etc please let me know 😉