Advanced Java Programming

Image
                          Advanced Java Programming Advanced Java Programming :-   Introduction to advance java   As most of us already know that if we want to make normal applications it  can be easily built using core Java concepts. But, when it we need to develop web applications, advanced Java fundamentals, like JSP, Servlets, JDBC etc. needed, so to add capabilities and features of the application advance java is essential for developers. Through the motive of this blog is to explain about Advanced Java, I will be giving you a complete insight into the fundamental concepts of Advance Java. Figure - 1.2 If you want to see complete video on this please  have a look the video below.                              Learn with Resh u Advanced Java Programming Course ...

How to use static keyword in Java


How to use static modifier in Java 




There are some non-access modifiers to achieve many other functionalities in java.
·      The static modifier for creating class methods and variables.
·  The final modifier for finalizing the implementations of classes, methods, and variables.
·      The abstract modifier for creating abstract classes and methods.
·      The synchronized and volatile modifiers, which are used for threads.



The Static Modifier

Static Variables

The static keyword is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists regardless of the number of instances of the class.
Static variables are also known as class variables. Local variables cannot be declared static.

Static Methods

The static keyword is used to create methods that will exist independently of any instances created for the class.
Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from those parameters, with no reference to variables.
Class variables and methods can be accessed using the class name followed by a dot and the name of the variable or method.
Example
The static modifier is used to create class methods and variables, as in the following example 
Class Employee.java

package com.statics.example;

public class Employee {

  String name ;
  static  String company = "Learn With Reshu";
  int id;
public Employee(String name,int id){
this.name= name;
this.id= id;
}
  
  
void display() {
System.out.println(this.name+"  "+company+" "+this.id);
}
}


in the above example there is a class name Employee having static variable   company 

Class Test.java

package com.statics.example;

public class Test {
public static void main(String[] args) {
Employee em = new Employee("reshu",  001);
Employee em1 = new Employee("reshu1",  002);
Employee em2 = new Employee("reshu2",  003);
Employee em3 = new Employee("reshu3",  004);
em.display();
em1.display();
em2.display();
em3.display();
}

}


When we make a object of Employee class in Test Class and call display method so we do not need to enter value of company variable as its static value is define in once Employee class so it can use in Test class directly. this is a advantage of static modifier that we do not need to assign values many times once it has been assign and that can be call directly by Class reference. 






Thanks for Watching !!!
Recommended Posts:

·       Overriding in Java
·       Overloading in Java
·       Interfaces in Java
·       Loops in Java
·       Variables in Java

Advanced Java Programming

         Abstraction in Java
·       Encapsulation in java
·       What is Java
·       Enabling Remote Debug

Comments

Popular posts from this blog

Learn Complete OOPS Concepts in one Project

Spring Core