Recent

3.Static Keyword in Java



Static keyword in Java is used to declare methods, inner classes and variables as static. one of the main purpose of static keyword is memory management. We can define these things as static:
1.    Data members
2.    Methods
3.    blocks
4.    Inner class
Once defined static, these things can be accessed without creating the object for the class in which they are present. As static methods gets the memory at compile time in class information(part of method area).

STATIC DATA MEMBER
·         If we declare data members of a class as static then it is known as static data member.
·         Static data members always gets memory in class information part of method area.
To understand the need and advantage of a static data member, consider the program below…

class Emp
{
String name, cname;
int salary;

void get(String s1, int s2, String s3)
{
name = s1;
salary = s2;
cname = s3;
}

void show()
{
System.out.println(name);
System.out.println(salary);
System.out.println(cname);
}

public static void main(String[] args)
{
Emp e1 = new Emp();
e1.get("abc", 101, "TCS");
e1.show();

Emp e2 = new Emp();
e2.get("xyz", 201, "TCS");
e2.show();
}
}


·          In the above program, every time we are creating an object the instance data member “cname” is getting a separate memory each time. And this variable is same for all the employee of a company.
·         So why we are giving memory to this variable each time an object is created.
·         To overcome this problem of memory management the variables are defined as static and Static data member are getting memory at class loading time always. So that they get the memory only once in life cycle of a class.

An Example of static data members:


class Emp
{
String name;
int salary;
static String cname = "TCS";

void get(String s1, int s2)
{
name = s1;
salary = s2;
}

void show()
{
System.out.println("Name: " +name);
System.out.println("Salary: " +salary);
System.out.println("Company name: "+cname);
}

public static void main(String[] args)
{
Emp e1 = new Emp();
e1.get("abc", 101);
e1.show();

Emp e2 = new Emp();
e2.get("xyz", 201);
e2.show();
}
}

/*Output

Name: abc
Salary: 101
Company name: TCS
Name: xyz
Salary: 201
Company name: TCS

*/
--------------------------------------------------------------------------------------------------------------------------
POINTS TO REMEMBER
·         Never use static things of a class via object of that class.
·         Always keep those properties of an object as a static data member of a class whose values are same for each object.
·         Static data member are getting memory at class loading time always. So that they get the memory only once in life cycle of a class.
·         All the objects are sharing the same memory location for each data member.
·         All the static things of a class always belongs to a class. They are treated as properties of a class.
·         All the non-static things of a class always belongs to objects. They are treated as properties of objects.
·         All the static data member can be accessed via class name.
--------------------------------------------------------------------------------------------------------------------------

STATIC MEMBER FUNCTION

static keyword is used for member functions(methods) to create a function which can be used without creating an object.
·         Always keep those behaviour of an object as a static functions of a class which are not doing a task related to any object.
·         Instance data member and instance memberfuntion of a class cannot be used directly in any static function of the same class. But they can be used after creating the object in thatfuntions.
·         Static data member and static member function of a class can be used directly in any function of the same class.

Example of static method:

class Demo
{

static void sum(int a, int b) //static method sum
{
System.out.println(a+b);
}

public static void main(String[] args)
{
int a = 10;
int b = 20;
sum(a,b);   //accessing the static method "sum"
}
}

/*Output
30
*/

Static Block

·         Static block is used to initialize the static data members dynamically.
·         Static block is always get executed at class loading time.
·         Static block is always executed only once in the life cycle of class.
To understand the static block, consider the following program:


class Temp
{
public static int x;

static
{
try
{
x=System.in.read(); //to read a line from keyboard
}
catch(Exception e)
{}
}}

class Temp1
{
public static void main(String[] args)
{
System.out.println(Temp.x);  //accessing static data member
}}


class Temp2
{
public static void main(String... s)
{
System.out.println(Temp.x);  //accessing static data member
}
}

·          In this program, we made two class withmain() method inside them.
·         We defined a static variable x at class level. But via static block, we will get different values of x each time the program executes.
·         So the need was something which can be run at class loading time. and we achieved this through static blocks. So static block made to dynamically allot the value to static data member.

No comments:

Post a Comment

Bug-dbug Designed by Bugdbug - Developed by Belson Raja Copyright © 2016

Theme images by Bim. Powered by Blogger.
Published By Bugdbug India