Public, Private and Protected:
Access specifier tells where that instance/ object/ method/ variable/ class can be accessible. public means method is open for all and it is accessible outside the class and even package. private means that instance is accessible only inside the class but will not be called outside from the class. This is for security issues. protected means that instance can be called from inside the class as well as from the class which is inheriting from this class ( inheritance is concept of OOP and we will read it in some other tutorial).
Static and Dynamic:
All methods/ objects/ variables/ instances can be of two types, static and dynamic. There are two major differences between these two terms:
- Static instances are independent from all objects of the class and can be called directly using class name whether we have to make object for dynamic instances.
- Static instances has its own fixed predefined memory but dynamic instances are assign new memory only when they are called. It saves memory because these occupy memory only during running time and after that memory becomes empty.
In this example, variable 'a' of class Check is static and variable 'b' is dynamic. In main function we are making two objects of class Check and are calling method test, which is increasing value of 'a' and 'b', 3 times for each object.
NOTE: Don't try to understand all concepts of the code, just look only on static and dynamic concept. We will read about other concepts later.
- package flowkl;
- class Check
- static int a = 2;
- int b = 3;
- public void test()
- System.out.printf("a is %d and b is %d \n",a,b);
- public class Tutorials
- public static void main(String args)
- Check first = new Check();
- Check second = new Check();
- System.out.println("for object first:");
- System.out.println("for object second");
- for object first:
- a is 3 and b is 4
- a is 4 and b is 5
- a is 5 and b is 6
- for object second
- a is 6 and b is 4
- a is 7 and b is 5
- a is 8 and b is 6
You can see that static variable is behaving same for static variable but different for dynamic variable. It is because static variable has fix memory location and doesn't allocated new memory for each new object as it is independent from all class objects while dynamic variable is assigning new location for each object.
Arguments vs Parameters:
Argument is the extra information which we will pass to a method. Parameter is the extra information that a method requires. The key difference is that arguments are passed during the execution and after passing they are parameters for that method.
Global variables vs Local variables:
The variables which are defined inside the class are called global variables and also known as class variables. Other variables which are defined inside any method/ conditional statement/ loop are called local variable of that method/ conditional statement/ loop.
Global variable are accessible everywhere inside the class except dynamic variables which are not accessible in static methods. Local variable are only accessible in their local environment (method/ conditional statement/ loop).