Total members 10261 | Gratitudes |It is currently Wed May 23, 2012 9:52 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Question
 Question subject: Static Methods and Variables
PostPosted: Sat Jan 31, 2009 12:18 am 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

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.

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 Which methods is best for SEO?
 methods with variable arguments list
 static vs fixed div position
 Calling Overloaded Methods
 PHP session variables problem in Firefox
 PHP session variables problem in Firefox
 signature creating in easy methods
 pass parameters to main method args[] variables
 Static Import
 Local variables vs Instance variables

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 3 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team