Static Keyword in Java

Advertisements

Final Keyword in Java This Keyword in Java

Static keyword in java

In Java, keywords are the reserved words that cannot be used as identifiers. In total there are 57 keywords in Java. One among them is Static.

The static keyword in Java is used mainly for memory management. It is used with variables, methods, blocks and nested classes. It is a keyword that is used to share the same variable or method of a given class. This is used for a constant variable or a method that is the same for every instance of a class. The main method of a class is generally labeled static. Lets start with static keyword in Java.

static keyword in java

Here we cover these Topics

No object needs to be created to use static variable or call static methods, just put the class name before the static variable or method to use them. A Static method can not call a non-static method.

In java language static keyword can be used for following

  • variable (also known as class variable)
  • method (also known as class method)
  • block
  • nested class

Static variable

If any variable we declared as static is known as static variable.

  • A Static variable is used to fulfill the common requirement. For Example the Company name of employees, the college name of students, etc. The Name of the college is common for all students.
  • The static variable allocates memory only once in the class area at the time of class loading.

Advantage of static variable

Using a static variable we make our program memory efficient (i.e it saves memory).

When and why we use static variable

Suppose we want to a store record of all employees of any company, in this case, employee id is unique for every employee but company name is common for all. When we create a static variable as a company name then only once memory is allocated otherwise it allocates a memory space each time for every employee.

Syntax for declare static variable:

public static variableName;

Syntax for declare static method:

public static  void methodName()
{
.......
.......
}

Syntax for access static methods and static variable

Syntax

className.variableName=10;
className.methodName();

Example

public static final double PI=3.1415;
public static void main(String args[])
{
......
......
}

Difference between static and final keyword

static keyword always fixed the memory that means that it will be located only once in the program whereas final keyword always fixed the value that means it makes variable values constant.

Note: As for as real-time statement there concern every final variable should be declared the static but there is no compulsion that every static variable declared as final.

Example of static variable.

In the below example College_Name is always same, and it is declared as static.

Example Static Keyword in Java

class Student
{
int roll_no;
String name;
static String College_Name="ITM";
}
class StaticDemo
{
public static void main(String args[])
{
Student s1=new Student();
s1.roll_no=100;
s1.name="abcd";
System.out.println(s1.roll_no);
System.out.println(s1.name);
System.out.println(Student.College_Name); 
Student  s2=new  Student();
s2.roll_no=200;
s2.name="zyx";
System.out.println(s2.roll_no);
System.out.println(s2.name);
System.out.println(Student.College_Name); 
}
}

Example

Output:
100
abcd
ITM
200
zyx
ITM

In the above example College_Name variable is commonly sharable by both S1 and S2 objects.

static keyword in java

In the above image, the static data variable is stored in the method area and a non-static variable is a store in java stack. Read more about this in JVM Architecture chapter

Why Main Method Declared Static ?

Because the object is not required to call static method if main() is non-static method, then JVM creates an object first then calls main() method due to that face the problem of extra memory allocation.

Static Method in Java

If you apply static keyword with any method, it is known as static method. A static method belongs to the class rather than the object of a class. It can be invoked without the need for creating an instance of a class. It can access static data member and can change the value of it.

Example of Static and non-Static Method

Example

class A
{
void fun1()
{
System.out.println("Hello I am Non-Static");
}
static void fun2()
{
System.out.println("Hello I am Static");
}
}
class Person
{
public static void main(String args[])
{
A  oa=new  A();
   oa.fun1(); // non static method 
   A.fun2();  // static method
}
}

Output

Hello I am Non-Static
Hello I am Static

Read more about these things in separate


Final Keyword in Java This Keyword in Java

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements