Private and
protected are examples of access modifiers. All in all, there are 4 types of access modifiers.
If a variable is
private, it means that it is only accessible inside the class where it is declared.
If a variable is
protected, it means that it is accessible not only inside the class where it is declared, but also to other classes which belongs to the same package as it is. A protected variable is also accessible to its subclasses.
You may visit the link provided by Campbell for the other access modifiers.
Dynamic initialization is when a variable's value is determined on runtime rather than on compile time.
For example,
The reason for this is because when the variables are compiled, the compiler already knows that the value of num1 is 2 and so it assigns the value 2 to num1. However, a compiler only compiles, but does not compute. When the compiler sees 1 + 1, instead of computing it and assigning the sum to num2, it assign 1 + 1 directly to num2 without computing it. Now, when num2 is run on the runtime, 1 + 1 will now be executed and it is the only time that the value of num2 which is 2 is determined.
Mezan Shareef wrote:one more doubt, lets say there is class 1 which has method 1. And there is class 2 which has some method. they are in same package. is it possible that i call Class2.method1. ?? i do not understand how ?
Yes, it is possible. In fact, you may access the methods of Class2 even though you're in a different package. One way to do it is what Campbell had shown. The other way is to declare the method as
static. A great example for this are the methods of the Math class. Ever wonder why you can easily access the methods (e.g. Math.abs(param)) of the Math class without the need to declare a variable of type Math? It's because they are declared as
static.