Static Keyword in Java

Google Advertisements

Final in Java This 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 is used in java 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.

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.

Static Keyword Example 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 in Java This in Java

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. If you want to post your article on our website/FB Group/FB Page please contact on hitesh.xc@gmail.com or 8076671483

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

Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer which are used in java programming. All these tools are available in bin folders so we set path upto bin folders. Difference between path and classpath in Java

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar. Difference between path and classpath in Java

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483 Try this Keyword C++ Programs