Total members 11890 |It is currently Fri Apr 19, 2024 11:04 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





What is a static method? What is a static variable?
This article is about "static" keyword usage in java and is divided into two main sections: static variables and static methods:

Static Variables:



A static variable is associated with the class as a whole rather than with specific instances of a class. Each object will share a common copy of the static variables i.e. there is only one copy per class, no matter how many objects are created from it. Class variables or static variables are declared with the static keyword in a class. These are declared outside a class and stored in static memory. Class variables are mostly used for constants. Static variables are always called by the class name. This variable is created when the program starts and gets destroyed when the programs stops. The scope of the class variable is same an instance variable. Its initial value is same as instance variable and gets a default value when its not initialized corresponding to the data type. Similarly, a static method is a method that belongs to the class rather than any object of the class and doesn't apply to an object or even require that any objects of the class have been instantiated.

Non-static variables take on unique values with each object instance.

You can define class variables as static members, as you knew static variables are shared between all the instances belonging to the same class. You can define class static variables using all the access providers :private, protected, public and default. Below is a class member static variable example where a new instances is create from class Animal and at the constructor of this class the static variable is incremented. At the end of main function the content of the static variable "numberOfAnimals" will be equal to the number of Animal class objects. See below:
Code:

public class StaticUsageExample 
{
       public static void main(String[] args) {
           
           Animal a1
=new Animal();
           Animal a2=new Animal();
           a1.printNumberOfAnimals();
           a2.printNumberOfAnimals();
           Animal a3=new Animal();
           a1.printNumberOfAnimals();
           a3.printNumberOfAnimals();
       }
}
class Animal 
{
    private static int numberOfAnimals;

    public Animal() {
        numberOfAnimals++;
    }
    public void printNumberOfAnimals()
    {
        System.out.println("Number of animals static counter ="+numberOfAnimals);
    }
    
}
 

The output of this snippet is :
Code:
Number of animals static counter =2
Number of animals static counter =2
Number of animals static counter =3
Number of animals static counter =3

You don't to create any instances of the class Animal to have access to its static variables, you can just using the class name as follows:
Code:

public class StaticUsageExample 
{
       public static void main(String[] args) {
            System.out.println("Number of animals in zoo is: "+Animal.numberOfAnimals);
       }
}
class Animal 
{
    public static int numberOfAnimals=4;
 
}
 

Remember: The static variables are created at start time.

Static methods:



Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object. A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can't override a static method with a non-static method. In other words, you can't change a static method into an instance method in a subclass.



The most famous static method in java is the main function :
Code:

   public static void main
(String[] args) {
             
       
}
 

It has to be static because It is the entrance of your java application. You can create your one static methods easily as follows :
Code:

public class StaticUsageExample 
{
       public static void main(String[] args) {
             Calculator.sum(5, 4);
       }
}
class Calculator 
{
    public static void sum(int x,int y)
    {
        System.out.println("The sum is:"+(x+y));
    }
 
}
 

Please note that you can't use non static class members (variables and methods) inside a static method, following codes give a compile error :
Code:

class Calculator 
{
    private int factor=4;
    public static void sum(int x,int y)
    {
        System.out.println("The sum is:"+(x+y)*factor);
    }
 
}
 

or
Code:

class Calculator 
{
 
    public static void sum
(int x,int y)
    {
        printValues(x+y));
    }
 
    public void printValues
(int z)
    {
        System.out.println(z);
    }
}
 

You even can't use the this. keyword inside a static function.



_________________
Please recommend my post if you found it helpful


Author:
Newbie
User avatar Posts: 14
Have thanks: 0 time

updated.

_________________
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  [ 2 posts ] 

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



Topic Tags

Java OOP
cron





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