- Concept of static is whenever a data which is common to objects is required.Just like C there are global variables.
- Keyword used is "static"
- In java static can be used in three scenarios:
- Variables
- Methods
- Block of code.
Static Variables:
- Static members are also called class variables
- Static variable are the one which is common to all objects of the class
- They are initialized at class loading time and when not initialized they will have the default value.
- We can access static variable with and without object of class
- Syntax: <Access specifier>static <datatype><variable name>
class Customer{
private int customerId;
private static int counter=100;
public Customer(){
customerId=++counter;
}
public int getCustomerId(){
return customerId;
}
}
class Retail{
public static void main(String args[]){
Customer custObj1= new
Customer();
System.out.println("Customer
Id:" + custObj1.getCustomerId());
Customer custObj2= new Customer();
System.out.println("Customer
Id:" + custObj2.getCustomerId());
}
}
Output will be :
Customer Id:101;
Customer Id:102;
As static is common to all objects and when object cusObj1 is created and default constructor incremented the customerId and it becomes 101 and same when other object custObj2 is created.
Static Method:
- Static method is a method which is common to entire class
- It loaded at class loading time
- It may be used for accessing static members
- Syntax: <access specifier> static <return type> <method name>(){}
- we can call static methods by creating an object or just by class name
- public static void myStaticMethod(){
}
we can call it either way take previous Customer class as example
Customer custObj1 = new Customer(); //creation of object
custObj1.myStaticMethod(); //calling static method by object custObj1
Customer.myStaticMethod(); //static method called by class name Customer
Note: Static member of class can be used in static and non-static methods
- Static method can only access other static members and static method
- Non-static variable can not be accessed directly inside static methods
Static Block:
- Static block is a block contains code statements which gets executed when class is first loaded.
- class Customer{
static{
code statements
............
}
}
- We can have any number of static block in our class
- They are used to initialization of static members just like constructor used to initialize instance variables of class.
- The order of execution of static block is in which the were written in the class
Summary:Static is a keyword and used as static variable and static block ,whenever we want global variable throughout the class static variable are used.
class Customer{
private static int counter;
private int customerId;
static{
counter=1000;
}
public Customer(){
customerId=++counter;
}
public int getCustomerId(){
return customerId;
}
}
class Retail{
public static void
main(String args[]){
Customer custObj1= new
Customer();
System.out.println("Customer
Id:" + custObj1.getCustomerId());
Customer custObj2= new
Customer();
System.out.println("Customer
Id:" + custObj2.getCustomerId());
}
}
Output:
Customer
Id: 1001
Customer
Id: 1002
Some of the questions you may encounter
solve them by your self and answer them by comments. I will be sharing the answers later
1. class Example{
private int x;
public Example(){
x=10;
}
public static void display (){
x++;
System.out.println(x);
}
public static void main(String arg[])
{
Example obj=new Example();
obj.display();
}
}
2.class Example{
public static int num;
static{
int num=100;
System.out.println(num);
}
public static void main(String arg[]){
System.out.println(num);
}
}
3. class Example{
public static int add(int a, int b){
return a+b;
}
public static void main(String arg[])
{
System.out.println(Example.add(100,200));
}
}
4.class Example{
static{
System.out.println("This gives the use of static blocks - This is Static Block 1");
}
public static void main(String args[]){
}
static{
System.out.println("Static block 2");
}
}
Thanks for reading my blogs
0 comments :
Post a Comment