Tuesday, May 8, 2018

static function in java best explained

static function in java

techsol expert 

Hello friends here i will explain you what is exaclty static function in java ,how to use it and what is advantages of it with example so let's start...


static function is a modifier in java which is in c++ language called access specifier but in java access specifier has changed into modifier but working motive is same.

"modifier defines access level of any class or function or variable.like public modifier,private modifier,protected modifier ,static etc."


java modifer ,acess specifier limit,java best modifier
Modifier access level 

static function in java watch live example



class Techsol {
 
  public static void main(String[] args) {
    display();  //calling without object
    Techsol t = new Techsol();
    t.show();  //calling using object
  }
 
  static void display() {
    System.out.println("Programming is amazing.");
  }
 
  void show(){
    System.out.println("Java is awesome.");
  }
}
we have taken a class called Techsol and 3 function with main one is static
 second is not static.if you have without static function you whenever you are 
you are calling function you need to create a object then you can call 
otherwise java IDE shows warning.
If you have static function then you can call that function directly without 
create any object.

like this if you have without static function socall like thisTechsol obj1;obj1.show();if you have static function so you can call like this Techsol.display();
example : - System.out.println();System is a class out is a String class's static variable therefore this is called directly with class then println is a static function therefore called directly.


Advantages of Static function

  1. if you use static modifier you you need less code and this is benefit of the static function.
      2. you don't need to create object .

No comments:

Post a Comment

Popular post

Techsol expert update Hello, guys now we have bought our personal domain and we serve help through our website. Students can also us...