Site icon SKLEARNING

Static member in java

java-static-keyword1_img

Static Variables:

Static variables are declared in the class usingStatic keywordStatic variables are by default initialized to its default valueStatic variables has a single copy for the whole class and does not depend on the objects

Static Function

Static functions defined inside the class are qualified with the keyword staticStatic functions can only access static members of the same classStatic function can be invoked using class name and dot operator

Static Class:

We have a class inside a class Which is Known as inner ClassInner class can be qualified with the keyword static.

Example:

public class Example

{

int x://instance member variable

static int x;// static member variable

public void fun1(){}//instance member 

public static void fun2(){y=4}// static member function}

static class Test

{

public static String country=”SKLEARNING”;

}

public static void main(String []args)

{

Example ex1=new Example();

Example ex2=new Example();

Example.y=5;//we also write y=5,because it is in same class

Example.Fun2();

Example.Test.country;

}

}

Exit mobile version