Introduction to Static keyword In Java

68 Shares

Static:-The multiple objects of the same class can be created and all the object contains the same property. However, all the object are independent of one another because the task on one object is not reflected the other object. Sometimes it is required to perform the task on the same variable by all the object. To implement this static has been introduced static is a keyword of the that is used to make the members static. A member having keywords static is called as a static member of the class.

Static keyword In Java

There are two type of static member:-
1) Static variable
2) Static method

1) Static variable:- Instance variable of the class having keyword static is called as a static variable of the class.

It has following properties

  1. It must have keywords static.
  2. It is the first member of the class to be called or created.
  3. It is created ever before the constructor call.
  4. It is automatically initialised by zero.
  5. It is created outside the object of the class.
  6. It is shared by all the object of the class.
  7. It can be accessed using the class name, object name or reference.
  8. It can be private or public member of the class.
  9. It is also called as a class variable.

2) Static method:- Method of the class having keyword static is called as a static method of the class.

It has following properties

a) Method having keyword static is called as a static method.
b) It is the first member of the class to be called.
c) It is called even before the constructor.
d) It is created outside the object of the class and shared by all the object of the class.
e) It is accessed using the class name, object and reference.
f) It can access only the static member of the class.

Variable in java:-

There are three types of variable in java.

1.Local variable
2.Instance Variable
3.Class Variable

1)Local variable:- Variable declared within the method of the class is called as a local variable. It is created when the method is called and it’s destroyed when the method call is terminated. It is accessible only within the method in which it is declared.

2)Instance variable:- declared within the method is called as an instance variable. It is created within the object of the class. It is accessible everywhere in the class.

3)Class variable:- Instance variable of the class having keyword static is called as a static variable or class variable. It is created before the creation of the first object and is destroyed when the main program is terminated. It is accessible by all the object of the class.

A class having main and other methods: –
A class may contain the main method and have one or more Method in the same class.

There are two ways to access the method

1) Using the static:- The method may be defined as static then within the main it is called directly because main itself is static and it can access only the static property of the class.

Example:-


2) Using the non-static:-The class may contain the main method as well as any other method which is non-static. Such method can’t be called within the main because main is static it can not access the non-static property of the class.
68 Shares

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.