Technology Programming

Static Fields



There can be times when it's useful to have values that are shared across all instances of a particular class. Static fields and static constants enable this to happen by belonging to the class and not to the actual objects.

The Static Modifier


Normally fields and methods defined in a class can only be used when an object of that class type has been created. For example, consider a simple Item class that keeps track of goods in a store:

public class Item {   private String itemName;  public Item(String itemName)   {     this.itemName = itemName;   }  public String getItemName()   {     return itemName;   } }
To be able to use the getItemName method we must first create an Item object (i.e., catFood):

public class StaticExample {  public static void main(String[] args) {     Item catFood = new Item("Whiskas");     System.out.println(catFood.getItemName());   } }
However, if the static modifier is included in a field or method declaration, no instance of the class is required for it to be used. The field or method is associated with the class and not an individual object. If you look back at the above example, you will see that the static modifier is already being used in the main method declaration:

public static void main(String[] args) {
The main method is a static method that does not require an object to exist before for it can be called. As its the starting point for any Java application, there are in fact no objects already in existence for it to be called from.

You could, if you felt like having a program that continually calls itself, do this:

public class StaticExample {  public static void main(String[] args) {    String[] s = {"random","string"};     StaticExample.main(s);     } }
Not very useful, but notice how the main method can be called without an instance of a StaticExample class.

What Is a Static Field?


Static fields are also know as class fields. They are simply fields that have the static modifier in their declarations. As mentioned before this means a static field is associated with the class rather than an object. For example, let's go back to the Item class and add a static field:

public class Item {  //static field uniqueId   private static int uniqueId = 1;  private int itemId;   private String itemName;  public Item(String itemName)   {     this.itemName = itemName;     itemId = uniqueId;     uniqueId++;   } }
The fields itemId and itemName are normal non-static fields. When an instance of an Item class is created these fields will have values that are held in that object. If another Item object is created then it too will have itemId and itemName fields for storing values. The uniqueId static field holds a value which will be the same across all Item objects. If there are a hundred Item objects, there will be a hundred instances of the itemId and itemName fields but only ever one uniqueId static field.

In the above example, uniqueId is being used to give each Item object a unique number. This is easy to do if every Item object that is created takes the current value in the uniqueId static field and then increments it by one. The use of a static field means that each object does not need to know about the other objects to get a unique id. This could be useful if you wanted to know the order in which the Item objects were created.

What Is a Static Constant?


Static constants are exactly like static fields except their values cannot be changed. In the field declaration the final and static modifiers are both used. For example, perhaps in the item class there needs to be a restriction on the length of the itemName. A static constant could be used:

public class Item {  private static int id = 1;   public static final int maxItemNameLength = 20;  private int itemId;   private String itemName;  public Item(String itemName)  {     if (itemName.length() > maxItemNameLength)     {       this.itemName = itemName.substring(0,20);     }     else     {       this.itemName = itemName;     }     itemId = id;     id++;   } }
As with static fields, static constants are associated with the class rather than an individual object:

public class StaticExample {  public static void main(String[] args) {    Item catFood = new Item("Whiskas");     System.out.println(catFood.getItemName());     System.out.println(Item.maxItemNameLength);     } }
There are two important things to notice about the maxItemNameLength static constant:
  • it is declared as a public field. Generally its not a good idea to have a public field in any class you design but in this case it doesn't matter. The value of the constant cannot be changed.
  • the static constant is used from the class name Item, not an Item object.

Static constants can be seen throughout the Java API. For example, the Integer wrapper class has two that store the maximum and minimum values an int data type can have:

System.out.println("The max value for int is: " + Integer.MAX_VALUE); System.out.println("The min value for int is: " + Integer.MIN_VALUE);Output: The max value for int is: 2147483647 The min value for int is: -2147483648
SHARE
RELATED POSTS on "Technology"
WordPress - How to Set up a New Theme to WordPress 3.
WordPress - How to Set up a New Theme to WordPress 3.
Solution of Creative Web Design
Solution of Creative Web Design
The three disciplines of User Experience
The three disciplines of User Experience
Web Design Sheffield Options For Professional Enterprises
Web Design Sheffield Options For Professional Enterprises
Do you have what it takes?
Do you have what it takes?
Segway Cost
Segway Cost
Microsoft Access 2010: What's Coming with Office 2010?
Microsoft Access 2010: What's Coming with Office 2010?
Companies of Web Development in Ireland Provide Designs that Work
Companies of Web Development in Ireland Provide Designs that Work
Penguin Update to Put Red-Flags on Negative SEO
Penguin Update to Put Red-Flags on Negative SEO
Innovative web 2 design templates can make your business famous quickly
Innovative web 2 design templates can make your business famous quickly
Building A Search Engine Friendly Website
Building A Search Engine Friendly Website
Exceptional Advice To Build Up Your Internet Marketing
Exceptional Advice To Build Up Your Internet Marketing
The Benefits of Selecting The Right Hosting Company
The Benefits of Selecting The Right Hosting Company
Is There a Methodology for Making Successful Logos
Is There a Methodology for Making Successful Logos
Benefits of Ruby On Rails Development
Benefits of Ruby On Rails Development
The Power of Colour and Shapes in Your Infant's Life.
The Power of Colour and Shapes in Your Infant's Life.
Advantages of Hiring PSD To HTML Service Providers
Advantages of Hiring PSD To HTML Service Providers
How to Make Responsive Web Design Attractive?
How to Make Responsive Web Design Attractive?
Converting PSD to Responsive HTML
Converting PSD to Responsive HTML
Just a few realy really hints and tips when it comes to website design but look for.
Just a few realy really hints and tips when it comes to website design but look for.

Leave Your Reply

*