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."
![]() |
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.
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
- 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