Ticker

6/recent/ticker-posts

Java (Static Keyword)

JAVA STATIC KEYWORD

In Java, the static keyword is used to declare a static variable, static method or a static class.

Static variable or function (method) can be access without any object, because these are directly related with its class.

JAVA STATIC VARIABLES
The static variables are defined as a class member and it can be accessed without an object of its class. Static variable need to  initialized only for once and it share by all the objects of its class. 

Note: all the object of the class having static variable will have the same instance of static variable static variable is used to represent common properties of a class therefore it saves memory.

Example:- suppose there are 50 students in XII class. All the students have their unique student id and name but class title will be
same for all the 50 students.
Here class title is common property for all. So if we create a class to store student details, then declare class_title field as static variable.

In the below class declaration, we have a simple class (student) with one static variable (class_title).

class Student
{
int rollno;
String name;
static String class_title="XII";
}

The static variable can be used to refer the common properties to all the objects (which is not unique for each object). for example, the company name of employees, college name of students, etc.
The static variable gets memory only once in the class area at the time of class loading.

ADVANTAGES OF STATIC VARIABLES
Static variables are memory saver. these are used to save the memory and makes programs more memory efficient.


JAVA STATIC VARIABLE EXAMPLE
class static_variable
{
int roll_no;
String name;

// Define class_title as static variable ...
static String class_title="XII";

//Define a parameterzied constructor ...
static_variable(int r, String n)
{
// Initiazing data members ...
roll_no=r;
name=n;
}

void display()
{
System.out.print("\n\nRoll number is: "+roll_no);
System.out.print("\nStudent name: "+name);
System.out.print("\nClass is: "+class_title);
}

public static void main(String args[])
{
// Calling the parameterized constructor ...
static_variable ob1=new static_variable(101,"Rakesh");
static_variable ob2=new static_variable(102,"Preeti");

// Calling the display function by ob1 ...
ob1.display();

// Calling the display function by ob2 ...
ob2.display();
}
}

- - - PROGRAM OUTPUT - - -


JAVA STATIC METHOD
In Java we can also declare a static method and like a static variable, we also don't require any object to access a static method.
main() method is the most common example of static method in Java programming language. 
main() method is declared as static because it is called before the any object of the class.

FEATURES OF JAVA STATIC METHOD
1. A static method only belongs to its class rather than object of its class.

2. A static method can be invoked/accessed without the need for creating an instance (object) of its class.

3. A static method can access the static data member and can change the value of static data member.


JAVA STATIC METHOD EXAMPLE
class static_method
{
// Define the msg() as static method
static void msg()
{
System.out.println("Hi, I am a static function ...");
}

public static void main(String[] args)
{
/* You can see the msg() method is calling
without an object becuase it is a static method */
 
msg();
    
}
}

- - - PROGRAM OUTPUT - - -
 










HOME

Post a Comment

0 Comments