on Leave a Comment

Static Members in Java Language

Static members are class level members. The class members which are defined with static keyword is called static members. There is only one copy of static member for a class, so static members do not depend on objects. Static members are mainly used for memory management. 

Java Static Members

(1) Static Variable
(2) Static Method
(3) Static Block
(4) Static Nested Class

Static Variable

The member variables which are defined with static keyword are called static variables. Static member variables are defined inside a class and outside all instance member methods, constructors, or any block. A static variable cannot be local variable, if we do so, we will get compile time error "illegal start of expression". 

Static variables get space in memory when class loading into JVM and is available till the class removed from JVM.

Static variables can be accessed with class name 

Syntax:

<class-name>.<variable-name>;

Example of static variable in java

public class StaticVariable
{
        static String course = "JAVA";
        private int roll_no;
        private String name;
        StaticVariable(String n, int r)
        {
                name = n;
                roll_no = r;
        }
        public void showDetails()
        {
                System.out.println("Name : "+name+"\nRoll no. : "+roll_no+"\nCourse : "+course);
        }
        public static void main(String[] args)
        {
                StaticVariable s = new StaticVariable("John", 32);
                s.showDetails();
        }
}

OUTPUT:

Name : John
Roll no. : 32
Course : JAVA

Static variables can be of any access modifier. 

Static Method

A static method always defined with static keyword. A static method belongs to class, not to objects. There is only one copy of static method for a class.  

Static methods cannot access instance member variables, they can only access static variables.  

Static methods get space in memory when class loading into JVM and is available till the class removed from JVM.

Static methods can be accessed with class name.

Example of static variable in java

public class StaticVariable
{
        static String course = "JAVA";
        private int roll_no;
        private String name;
        StaticVariable(String n, int r)
        {
                name = n;
                roll_no = r;
        }
        public void showDetails()
        {
                System.out.println("Name : "+name+"\nRoll no. : "+roll_no+"\nCourse : "+course+"\n");
        }
       
        static void changeCourse()
        {
                course = "C#";
        }
        public static void main(String[] args)
        {
                StaticVariable s1 = new StaticVariable("John", 32);
                s1.showDetails();
               
                StaticVariable.changeCourse();
                StaticVariable s2 = new StaticVariable("Andrew", 10);
                s2.showDetails();
        }
}

OUTPUT:

Name : John
Roll no. : 32
Course : JAVA

Name : Andrew
Roll no. : 10
Course : C#

Static Block

Static block is defined with static keyword. Static block is used to initialize static members. Static block executes before main() function, when class is loading into JVM. 

Example of static variable in java

public class StaticVariable
{
        static String course;
        private int roll_no;
        private String name;
        StaticVariable(String n, int r)
        {
                name = n;
                roll_no = r;
        }
        public void showDetails()
        {
                System.out.println("Name : "+name+"\nRoll no. : "+roll_no+"\nCourse : "+course+"\n");
        }
       
        static void changeCourse()
        {
                course = "C#";
        }
        public static void main(String[] args)
        {
                StaticVariable s1 = new StaticVariable("John", 32);
                s1.showDetails();
               
                StaticVariable.changeCourse();
                StaticVariable s2 = new StaticVariable("Andrew", 10);
                s2.showDetails();
        }
        static
        {
                course = "JAVA";
        }
}

OUTPUT:

Name : John
Roll no. : 32
Course : JAVA

Name : Andrew
Roll no. : 10
Course : C#

Static Nested Class

A nested class can be defined with static keyword. 

Example of static nested class in java

public class StaticVariable
{
        static class mainClass
        {
                static String course = "JAVA";
            private int roll_no;
            private String name;
            mainClass(String n, int r)
            {
                name = n;
                roll_no = r;
        } 
            public void showDetails()
            {
                   System.out.println("Name : "+name+"\nRoll no. : "+roll_no+"\nCourse : "+course+"\n");
            }
       
            static void changeCourse()
            {
                course = "C#";
            }
        }
        public static void main(String[] args)
        {
                StaticVariable.mainClass s1 = new StaticVariable.mainClass("John", 32);
s1.showDetails();
               
                StaticVariable.mainClass.changeCourse();
                StaticVariable.mainClass s2 = new StaticVariable.mainClass("Andrew", 10);
                s2.showDetails();
        }
}

OUTPUT:

Name : John
Roll no. : 32
Course : JAVA

Name : Andrew
Roll no. : 10
Course : C#

0 comments:

Post a Comment