Static members are common for all the instances
(objects) of the class.
It is a
keyword that are used for share the same variable or method of a given class.
There is no need to create object to access static
variable or static methods because static variable and method is accessed by class
name
Static class can access only static data members
or member functions.
Non static class can access both static and
non-static data members(variables) and member functions(methods or functions).
Use static keyword before declaring variable, class,
method and block to make it static.
Static keyword is used for memory management.
Static method cannot call non-static method.
Uses:
static keyword can be used for following
·
variable (also known as
class variable)
·
method (also known as
class method)
·
block
·
nested class
static variable
A variable
is called as static variable if static keyword is used before variable name.
static variable is known as
class variable.
static variable can be used
to refer the common property of all objects e.g. Bank name of employees, college name of
students etc.
The static variable gets memory only once in
class area at the time of class loading.
Advantage
It saves memory because static variable gets
memory only once in class area at the time of class loading.
When and why we use
static variable
Suppose we want to store record of all employee
of any Bank, in this case employee id is unique for every employee but Bank
name is common for all. When we create a static variable as a Bank name then it
will allocate memory only once.
Syntax:
static datatype variablename;
Example: static int x;
Example:
public class Bank
{
int id;
String name;
static String Bank_Name="SBI";
}
class StatDemo
{
public static void main(String args[])
{
//Print 1st employee information
Bank b1=new Bank();
b1.id=1010;
b1.name="Ajinkya";
System.out.println(b1.id);
System.out.println(b1.name);
System.out.println(Bank.Bank_Name); //Bank_Name
is static so it is accessed by class
name
//Print 2nd employee information
Bank
b2=new Bank();
b2.id=1011;
b2.name="Hemant";
System.out.println(b2.id);
System.out.println(b2.name);
System.out.println(Bank.Bank_Name);
}
}
Output:
1010
Ajinkya
SBI
1011
Hemant
SBI
static method
A method is called as static if static keyword is used before
method name.
static method is called as class method and it called by class
name.
No need to create an object to call static method.
static method can access static data member and
can change the value of it.
Syntax:
static void methodname ()
{
//
}
Example:
static void display()
{
//
}
Static
method can access only static members and it is called by class name.
Problem:
public class Bank{
String name="DEV";
static String
Bank_Name = "SBI";
static void
display(){
System.out.println("name is:"+name+"\n"+" Bank:
"+Bank_Name);
}
public
static void main(String args[]){
Bank.display(); //display method is called by class name because it static method
}
}
Output:
/Bank.java:7: error: non-static variable name cannot be
referenced from a static context
System.out.println("name is:"+name+"\n"+" Bank:
"+Bank_Name);
^
1 error
Note:
It displays error because name is non static data member
.so static method (display) cannot access non static data member like name.
Solutions:
public class Bank{
static String name="DEV";
static String
Bank_Name = "SBI";
static void
display(){
System.out.println("name
is:"+name+"\n"+" Bank: "+Bank_Name);
}
public static void main(String args[]){
Bank.display(); //display method is called by class name because it static method
}
}
Output:
name is:DEV
Bank: SBI
static block
It is used to initialize the static data member.
It is executed before main method at the time of
class loading.
Example:
public class LearningPoint92{
static{System.out.println("static block Example");}
public
static void main(String args[]){
System.out.println("Welcome to LP");
}
} .
Output:
static block Example
Welcome to LP
Why java
main method is static?
A method which is called by
class name is called as static method. main method is called by JVM and it is
called before any object creation so object is
not required to call static method.it saves memory.
Can we execute a program without main () method?
Yes, using static block we can
execute a program without main () method.
Example:
public class
LearningPoint92{
static{
System.out.println("It is called by without using main
method");
System.exit(0);
}
}
Output:
It is called by without using
main method.
Note:
We can execute a program without main
() method in previous version of JDK ,
not in jdk7 and above.
No comments:
Post a Comment