Static keyword

             static keyword in Java is used to create a variable which belongs to the class rather than an individual instance or object. It is basically used for memory management.

Note: Only a single copy of member is maintained across all of the instances of the class.
    Example:
            class User{
                public static int noOfUsers;
                public int userId;
                public String userName;
                
                public User(int userId, String userName){
                    noOfUsers +=1;
                    this.userId = userId;
                    this.userName = userName;
                }
            }

            class Program{
                    pubic static void main(String args[]){
                        User user1 = null;
                        User user2 = null;

                        System.out.println("Before assigning an object noOfUsers are: " + User.noOfUsers);
                        user1 = new User(1, "Joey");
                        user2 = new User(2, "Ross");

                        System.out.println("After assigning object noOfUsers are: " + User.noOfUsers);
                        //Note: Value of static variable will be same for all class instances
                        System.out.println("Id: " + user1.userId + " and noOfUsers val: " + user1.noOfUsers);
                        System.out.println("Id: " + user2.userId + " and noOfUsers val: " + user2.noOfUsers);
                    }
            }

            Output:
                Before assigning an object noOfUsers are: 0
                After assigning object noOfUsers are: 2
                Id: 1 and noOfUsers val: 2
                Id: 2 and noOfUsers val: 2

            Note: You can access static variable using reference variable Ex. user1.noOfUsers but it is recommended to call static variables or methods using the class name iteself as User.noOfUsers.

Static Method:
        These are class level methods. A static method can be accessed without creating new instance of a class and can be invoked using the class name iteself. 
        Example:
            class User{
                public static int noOfUsers;
                public int userId;
                public String userName;
                
                public User(int userId, String userName){
                    noOfUsers +=1;
                    this.userId = userId;
                    this.userName = userName;
                }

                public static void sayHello(){
                       System.out.println("Static method is called...");
                }

                public void printName(){
                        System.out.println("User's name is " + this.userName);
                }
            }

             class Program{
                    pubic static void main(String args[]){
                        User user = new User(1, "Joey");
                       //You can call static method using either Class name(recommended) or using reference variable
                        user.sayHello();  //using reference variable
                        User.sayHello(); //using class name

                        //You cannot call a static method without creating an instance
                        //User.sayHello();  this line will throw an error
                        user.printName();  //User's name is Joey
                    }
            }

Static block:
           We can also create a static block in java using static keyword. 
            Static blocks are executed before main method and constructors when class loads. Usually, it is used to initialize static variables.
Note: static block cannot return a value and only static variables can be used inside. Using non-static variables will result in compilation error.

            Example:
                class User{
                        static{
                                System.out.println("Static invoked before constructor and main method");
                        }
    
                        //constructor
                        public User{
                                System.out.println("Constructor called...");
                        }
                }

                class Program{
                        public static void main(String args[]){
                                User user = new User();
                        }
                }

            Output:
                    Static invoked before constructor and main method
                    Constructor called


Continue Learning: final keyword



Comments

Popular Posts