- static
- The keyword "static" is used to declares a class variable rather than an instance variable. You also use static to declare class methods.
Instance Variables
- When you declare a member variable such as "name" in class Fruit:
class Fruit { public String name; }you declare an instance variable.
Every time you create an instance of a class, the runtime system creates one copy of each the class's instance variables for the instance.
You can access an object's instance variables from an object as shown in the following example: Fruit orange = new Fruit(); // create an object
Class Variables
orange.name = "Florida Orange";
// refer to orange's nameFruit apple = new Fruit(); // create an object
apple.name = "Red Delicous";
// refer to apple's name
Instance Methods and Class Methods
- Instance variables are in contrast to class variables (which you declare using the static modifier).
class Fruit
{
public static int total_fruit_count = 0;
// count total number of fruit instancespublic String name;
//--- constructor ----
public Fruit (String new_name)
{
name = new_name;
total_fuit_count = total_fruit_count+1;
}
}
- The runtime system allocates class variables once per class regardless of the number of instances created of that class.
- The system allocates memory for class variables the first time it encounters the class. All instances share the same copy of the class's class variables. You can access class variables through an instance or through the class itself.
Here we will use an example Test_Fruit_Class program to demonstrate this:
//---------------------------------
// Test if the Fruit class works.
//---------------------------------
class Test_Fruit_Class
{
public static void main (String[] args)
{
System.out.println("When Fruit class has no instance, " +
"Fruit.total_fruit_count = " + Fruit.total_fruit_count);Fruit orange = new Fruit("Florida Orange");
System.out.println("After Fruit class has 1 instance (i.e., orange), "
+ "Fruit.total_fruit_count = " + Fruit.total_fruit_count);Fruit apple = new Fruit("Red Delicious");
System.out.println("After Fruit class has 2 instance "
+ "(i.e., orange and apple), Fruit.total_fruit_count = "
+ Fruit.total_fruit_count);
}
}Note here that Fruit is a class, and you can access the class variable total_fruit_count through the class itself (Fruit.total_fruit_count) or through an object (apple.total_fruit_count).
- Why class variables?
You use class variables for items that you need only one copy of and which must be accessible by all objects inheriting from the class in which the variable is declared.
Methods are similar: Your classes can have instance methods and class methods.
- By default, unless otherwise specified, a member declared within a class is an instance member.
- To specify that a method is a class method, use the static keyword in the method declaration.
Class Method Example: main()Class Method Example: Integer.parseInt(String s) and Math.floor(...)
The main method of a program must be a class method. This method belongs to the class as a whole, not to any particular object.
public static void main (String[] args)
{ //... }
To extract an integer from a string, we use
Restriction on Accessing Instance and Class Members
int i = Integer.parseInt ("5"); // this gives us i=5.
To truncate a floating point number to get the integer portion, we use
int i = (int) Math.floor (5.2); // this gives i=5.
Math and Integer are classes that simply group together useful methods. The Integer.parseInt and Math.floor methods are class methods which we can call without creating instance objects of the class.
- Instance methods
- operate on the current object's instance variables but
- also have access to the class variables.
- Instance methods are accessible only through an object.
- Class methods, on the other hand,
- cannot access the instance variables declared within the class (unless they create a new object and access them through the object).
- Class methods can only operate on class variables.
- Also, class methods can be invoked from the class itself, you don't need an instance to call a class method.
Instance Methods Class Methods can acess
instance variablesyes no can access
class variablesyes yes can be accessed through only objects objects or the class itself
Here is an example program which uses "static" variable and methods to read String inputs.//-------------------------------------
// Read_color:
// Use static class variable
// static BufferedReader in
// to read color name input.
//-------------------------------------import java.io.*;
class Read_color
{
//---------------
// Variables
//--------------
static BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));//-------------
// Methods
//-------------
public static void main (String[] args) throws IOException
{
String color;color = get_color();
System.out.println ("The color you entered is -- "
+ color);color = get_color();
System.out.println ("The color you entered is -- "
+ color);
}public static String get_color() throws IOException
{
System.out.print("Enter the name of a color --> ");
String line = in.readLine();
return line;
}
}
In the Fruit class example above, we used the constructor to initialize the instance variables for each new object.There are a number of advantages to this programming practice --
class Fruit
{
public static int total_fruit_count = 0;
// count total number of fruit instancespublic String name;
//--- constructor ----
public Fruit (String new_name)
{
name = new_name;
total_fuit_count = total_fruit_count+1;
}
}
- All of the initialization code is in one place, thus making the code easier to maintain and read.
- Defaults are handled explicitly.
- Constructors are widely understood by the Java community, including relatively new Java programmers.