Selenium Webdriver Day 2 : Java basic keyword understanding (static, final, access specifier etc)

In the previous article as we learned about how to create class and object now our objective is to learn basic java programming, to learn this first we must have basic knowledge of java keywords which we are going to use mostly in our programming, Following are basic keywords :
1. static keyword
2. public , private , protected and default(access specifier)
3. final keyword


1. Static keyword : Most of the time we see the method or variables are defined as static in our java program, Basically static keyword perform 3 tasks
  • If a variable is defined as static , JVM(java virtual machine) allocates memory to static variable before the creation of any object. 
  • When a variable or method is defined as static, only single copy will created in JVM for that variable, It will not allow you to create duplicate copy for same variable. If all the classes sharing same copy of variable and any user will update the value of that variable then updated value will displayed in all over the project.
  • If a variable or method is defined as static , we can call method or variable just by "Classname.method_name" , That means we do not need to create object to call all those methods which is defined as static.
Check below program to understand this concept deeply :

public class Demo {
//defining variables
String name="Sachin";
static float balance=10000.00f;
//defining methods
public void saySonething() {
System.out.println("Hello");
}
public static void main(String[] args) {
Demo d1=new Demo(); // creating first object of Demo class
Demo d2=new Demo(); // Creating second object of Demo class
Demo d3=new Demo(); // Creating third object of Demo class
d1.balance=2000.00f; // update value of balance with d1 object
System.out.println("Updated balance is = "+d3.balance); // printing value of banalce using d3 object
}
}
As we discussed above JVM will create single copy of variable which is defined as static. Here we have create balance as a static variable so jvm will create single copy for this in memory. Inside the main method we have created 3 object for same class to know either variable is sharing single copy or not. With the help of d1 variable we have updated the value of balance variable to 2000.00f. Now we print the value of balance with the help of d3 object . Output of above program is  

Updated balance is = 2000.0
Because it share single copy so once we updated the value of balance with d1 object ,it will reflect the same value in all over the program.

Real Life use : Lets have a look on banking application . In bank their are 3 methods of transaction first by net banking, second by directly go to bank branch to deposit the money , third by using ATM.
Suppose all three process is happening at the same time. In this case balance variable must be declared as static so all three transaction share same copy of balance variable and value will be updated accordingly.


2. Access Specifier : Access specifier is used to restrict the outside users from accessing out code. It provide security to the program and it will show only limited data to third party user depends upon the access specifier selected.
Example us real life : Suppose we are working on banking application. Now a person from any e-commerce side purchased something and go to bank website for the payment process. Now this whole process will be done via API. e-commerce site will hit the api and bank will provide the data to them.
When during the payment process when third party user hit that api  for payment we have to define only one methods as public so third party can get balance from bank and all other methods inside that class will be declared as private so that they can not access other part of our program, We will understand this later when we will work on Abstraction.

In java 4 basic types of access specifiers are used - public- default- private and protected.

 Public : All the information will be available to any user either that is inside the project or outside  the project.

 Private : Information should not be available outside the class. Information will be shared within the  same class only.

 Protected : Information is shared inside the same project. We can not share the information outside  the project.

 Default : Once we defined class as it means we can share the share information  within the same  class and same package only, we can not share information with other package and other projects.



3.Final Keyword : With the help of Final keyword we can not change the value in runtime and compile time.Final keyword have different role with methods , variables and class. Check this below :

  • When we create a variable as final then we can not change value of the variable,
  • If we mark or declared method as final it means we can not override that methods.
  • If class is declared as final it means we can not inherit that class.
Example : final static float balance=10000.00f;
Here variable balance is  defined as final so we can not change value of that in entire program in run time and compile time.




Popular posts from this blog

Selenium Webdriver Day 4 : Inheritance

Selenium Webdriver Day 1 : Class and object

Python Programming - Log file is not capturing the data?