Static keyword in java is mainly used for memory management.this is mainly used for variable,methods and blocks.
This explain you how static variable works , if you have some more time just see it.
Simple Static example
public class studentdetails {
int age ;
String name;
static String city="fremont";
studentdetails(int age,String name)
{
this.age=age;
this.name=name;
}
public static void hello(){ //this is a static method
city = "foster";
System.out.println("My city is "+city);// Static method can use only static variable
}
public void test(){
System.out.println("My Name is -->"+name);
System.out.println("My age is -->"+age);
System.out.println("City is -->"+city);
}
// static method can use only static variable
public static void main(String[] args) {
studentdetails obj1 = new studentdetails(22,"Ram");
studentdetails obj2 = new studentdetails(19,"krishna");
obj1.test();//variable city is loaded only once in class so memory
obj2.test();//second time also when object is called static variables memory remains same
hello();
}
}
Output:-
My Name is -->Ram
My age is -->22
City is -->fremont
My Name is -->krishna
My age is -->19
City is -->fremont
My city is foster
Static method is used by putting a static keyword after access modifier in method as seen in above example.
Example : this program shows difference of creating object with static and non-static variable, expense of room mates total and personal for different students.
Room(){
public void addExpense(double amount){
public static void main(String[] args) {
Room obj1=new Room();
- Java static variable : It is used to refer common property of all the objects. As name of city which is common for all the entity.
- Static variable gets memory at time of class load.
This explain you how static variable works , if you have some more time just see it.
For videos in hindi click here
Simple Static example
public class studentdetails {
int age ;
String name;
static String city="fremont";
studentdetails(int age,String name)
{
this.age=age;
this.name=name;
}
public static void hello(){ //this is a static method
city = "foster";
System.out.println("My city is "+city);// Static method can use only static variable
}
public void test(){
System.out.println("My Name is -->"+name);
System.out.println("My age is -->"+age);
System.out.println("City is -->"+city);
}
// static method can use only static variable
public static void main(String[] args) {
studentdetails obj1 = new studentdetails(22,"Ram");
studentdetails obj2 = new studentdetails(19,"krishna");
obj1.test();//variable city is loaded only once in class so memory
obj2.test();//second time also when object is called static variables memory remains same
hello();
}
}
Output:-
My Name is -->Ram
My age is -->22
City is -->fremont
My Name is -->krishna
My age is -->19
City is -->fremont
My city is foster
Static method is used by putting a static keyword after access modifier in method as seen in above example.
- Static method uses static variable and can also change value of it.
- Static method can be call without creating instance of class,as it belongs to class
Example : this program shows difference of creating object with static and non-static variable, expense of room mates total and personal for different students.
program
public class Room {
static double totalExpense;
double localexpence=0;
Room(){
}
public void addExpense(double amount){
totalExpense=totalExpense+amount; // method use static variable to add total expense
System.out.println("total is "+totalExpense);
}
public void addnewexpence(double amount){
localexpence=localexpence+amount; // it uses non-static variable to add personal expense
System.out.println("personal expence is "+localexpence);
}
//every time when object is created new memory is allocated to non-static variable
// static variable shares common memory , so not invoked when instance is created
public static void main(String[] args) {
Room obj1=new Room();
obj1.addExpense(20d);
obj1.addnewexpence(40d);
Room obj2=new Room();
obj2.addExpense(50d);
obj2.addnewexpence(60d);
Room obj3=new Room();
obj3.addExpense(40d);
obj3.addnewexpence(70d);
}
}
Output:-
total is 20.0
personal expence is 40.0
total is 70.0
personal expence is 60.0
total is 110.0
personal expence is 70.0
No comments:
Post a Comment