Total members 11890 |It is currently Thu Apr 18, 2024 7:27 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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.



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  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     -  
 Static Import     -  
 What is a static method     -  
 Class static properties     -  
 static vs fixed div position     -  
 static variable defined in function     -  
 Calling a local variable from a static function     -  



Topic Tags

Java OOP, Java Basics






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com