Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Static Methods and Variables

Sat Jan 31, 2009 12:18 am

Static Methods and Variables :
All class instances share the static variables and methods .They are declared using the "static" modifier. The static modifier can be applied to method, variable or a block. All the instances see the changes in the static variable.

Static and non static methods can access the static variables.

Code:

public class Main 
{
 
    public static void main
(String[] args) {
       
         BMW_H5 car1
=new BMW_H5("Owner1");
         BMW_H5 car2=new BMW_H5("Owner2");
         
         System
.out.println("Car 1 owner "+car1.getOwnername());
         System.out.println("Car 1 : salary "+car1.getSalary());
         System.out.println("Car 2 owner "+car2.getOwnername());
         System.out.println("Car 2 : salary "+car2.getSalary());
         BMW_H5.setSalary(6999.0f);
           System.out.println("Car 1 owner "+car1.getOwnername());
         System.out.println("Car 1 : salary "+car1.getSalary());
         System.out.println("Car 2 owner "+car2.getOwnername());
         System.out.println("Car 2 : salary "+car2.getSalary());
         
         
    
}

}
 

class BMW_H5
{
    private static float salary=5000.0f;
    private String ownername;
    public BMW_H5(String ownername)
    {
        this.ownername=ownername;
    }
    public static float getSalary() {
        return salary;
    }

    public static void setSalary(float salary) {
        BMW_H5.salary = salary;
    }

    public String getOwnername() {
        return ownername;
    }

    public void setOwnername(String ownername) {
        this.ownername = ownername;
    }
    
    
    
    
}
 


The output of this code is :
Code:
Car 1 owner Owner1
Car 1 : salary 5000.0
Car 2 owner Owner2
Car 2 : salary 5000.0
Car 1 owner Xonwer1
Car 1 : salary 6999.0
Car 2 owner Xonwer2
Car 2 : salary 6999.0

In the code above when i changed the value of static variable "salary", it changed for both .But every one has its own "ownername" variable.

Note: the static variable can be accessed by the class name or the instance of the class.

Same as static variable , the static method is come for all instances of the class. Static method can't access non static methods and variables in the class. Static functions can be called using the class name without the need to create any instance. The most well known example about static methods is the main function which is used to run your java application.


Code:
public class Main {
    public String myname="Tom";

    public static void main(String[] args) {

         System.out.println("myname = "+myname);

    }
}
 


The code above will not compile ,because the variable myname is not static .

Example on the static block of code :

The following example has a static block of code .

Code:
public class Main {
    public static int x=0;
    static 
    
{
        x++;
        System.out.println("static block  x="+x);
    }
     
            
    public static void main
(String[] args) {

          Main obj=new Main();
          System.out.println("main function x="+x);

    }
}
 


The steps is the following :
  1. x is initialized by 0.
  2. static block is executed .


The output of the program :
Code:
static block  x=1
main function x=1


The static block is executed just one ,in the class loading . And not in the creation of an instance.



Post a reply
  Related Posts  to : Static Methods and Variables
 Can we override static methods in java     -  
 main method and two static methods and loop until input zero     -  
 difference between a static and a non-static inner class     -  
 Local variables vs Instance variables     -  
 What is a static method     -  
 Static Import     -  
 Class static properties     -  
 static vs fixed div position     -  
 static variable defined in function     -  
 Calling a local variable from a static function     -  

Topic Tags

Java Basics, Java OOP