“static” – KEYWORD

Why “static”:

The static keyword in java is used for the memory management mainly.

static keyword is used to save our time and typing.

static variables:

static variable will have only one copy which will be shared by all objects.

EXAMPLE:

public class Employee
{
int empid;
String empname;
double salary;
char grade;
static String companyName=”HCL”;
public static void main(String args[])
{
println(Employee. CompanyName);
}
}

static method:

Static methods do not use any instance variables of any object of the class they are defined in.

Rules for “static” methods:

static methods can’t use instance variables/ constants. ie) static methods can use static or local variables/constants.

static method are maintained at class level. So, they doesn’t require object.

static method call happens with the help of class name.

static method cannot use instance variables but they can use static and local variables or constants.[TBD]

Example for static methods: helper methods, utility methods [TBD]

EXAMPLE:

public class Emp
{
static int m() //static method
{
}
int n() // non- static method
{
}
public static void main(String args[])
{
Emp.m(); // static method call
Emp e1= new Emp(); // when we use static keyword
e1.n();
}
}

Leave a comment

Design a site like this with WordPress.com
Get started