Tuesday
Java ProgrammingJava Instance and Static Members
Basic of Java Programming
- Basics of Java Programming
- Objects of Java
- Java Instance and Static Members
- Inheritance and Aggregation in Java Language

Each object made will have its own copies of the fields defined in its class. The fields of an object are called instance variables. The principles of the instance variables in an object comprise its state. Two evident stuff can have the same state, if their instance variables have the same principles. The methods of an object define its behavior. These methods are called instance methods. It is vital to note that these methods pertain to each object of the class. This must not be confused with the implementation of the methods, which is shared by all instances of the class. Instance variables and instance methods, which be in the right place to stuff, are collectively called instance members, to distinguish them from static members, which only be in the right place to the class.
Invoking Methods
Stuff communicate by message passing. This means that an object can be made to exhibit a particular behavior by invoking the appropriate operation on the object. In Java, this is done by calling a method on the object using the binary infix dot ‘.’ operator. A method call spells out the complete message: the object that is the receiver of the message, the method to be invoked, and the arguments to the method, if any. The method invoked on the receiver can also send information back to the sender, via a return value. The method called must be one that is defined for the object.
CharStack stack = new CharStack(5); // Start a stack
stack.push('J'); // (1) Character 'J' pushed
char c = stack.pop(); // (2) One character popped and returned: 'J'
stack.printStackElements(); // (3) Compile time error: No such method in CharStack
The sample code above invokes methods on the object denoted by the reference variable stack. The method call at (1) pushes one character on the stack, and the method call at (2) pops one character off the stack. Both push() and pop() methods are defined in the class CharStack. The push() method does not return any value, but the pop() method returns the character popped. Trying to invoke a method printStackElements() on the stack results in a compile-time error, as no such method is defined in the class CharStack.
The dot ‘.’ notation also can be used with a reference to access fields of an object. The use of the dot notation is governed by the accessibility of the member. The fields in class CharStack have private accessibility, indicating that they are not accessible from outside the class:
stack.topOfStack++; // Compile time error: topOfStack is a private field.
In some cases, certain members must only be in the right place to the class, and not be part of any object made from the class. An example of such a situation is when a class desires to keep track of how many stuff of the class have been made. Defining a counter as an instance variable in the class definition for tracking the number of stuff made, does not solve the problem. Each object made will have its own counter field. Which counter must then be simplified? The solution is to declare the counter field as life static. Such a field is called a static variable. It belongs to the class, and not to any object of the class. A static variable is initialized when the class is loaded at runtime. Also, a class can have static methods that be in the right place only to the class, and not to any stuff of the class. Static variables and static methods are collectively known as static members, and are distinguished from instance members in a class definition by the keyword static in their declaration.

Figure 1.4. Class Diagram Showing Static Members of a Class
Figure 1.4 shows the class diagram for the class CharStack. It has been augmented by two static members that are shown underlined. The augmented definition of the CharStack class is given in Example 1.2. The field counter is a static variable declared at (1). It will be allocated and initialized to the default value 0 when the class is loaded. Each time an object of the CharStack class is made, the constructor at (2) is executed. The constructor explicitly increments the counter in the class. The method getInstanceCount() at (3) is a static method belonging to the class. It returns the counter value when called.
Figure 1.5 shows the classification of the members in class CharStack using the terminology we have introduced so far. Table 1.1 at the end of this part, provides a summary of the terminology used in defining members of a class.
Example 1.2 Static Members in Class Definition
// Source Filename CharStack.java
public class CharStack {
// Instance variables
private char[] stackArray; // The array implementing the stack.
private int topOfStack; // The top of the stack.
// Static variable private static int counter; // (1)
// Constructor now increments the counter for each object made.
public CharStack(int capacity) { // (2)
stackArray = new char[capacity];
topOfStack = -1;
counter++;
}
// Instance methods
public void push(char element) { stackArray[++topOfStack] = element; }
public char pop() { return stackArray[topOfStack--]; }
public char peek() { return stackArray[topOfStack]; }
public boolean isEmpty() { return topOfStack < 0; }
public boolean isFull() { return topOfStack == stackArray.length - 1; }
// Static method (3)
public static int getInstanceCount() { return counter; }
}

Figure 1.5. Members of a Class
Clients can access static members in the class by using the class name. The following code invokes the getInstanceCount() method in the class CharStack:
int count = CharStack.getInstanceCount(); // Class name to invoke static method
Static members can also be accessed via object references:
CharStack stack1 = new CharStack(10);
int count1 = stack1.getInstanceCount(); // Reference invokes static method
Static members in a class can be accessed both by the class name and via object references, but instance members can only be accessed by object references.
Terminology for Class Members Instance Members
These are instance variables and instance methods of an object. They can only be accessed or invoked through an object reference.
Instance Variable
A field that is allocated when the class is instantiated, that is, when an object) of the class is made. Also called non-static field.
Instance Method
A method that belongs to an instance of the class. Stuff of the same class share its implementation.
Static Members
These are static variables and static methods of a class. They can be accessed or invoked either by using the class name or through an object reference.
Static Variable
A field that is allocated when the class is loaded. It belongs to the class and not to any object of the class. Also called static field and class variable.
Static Method
A method which belongs to the class and not to any object of the class. Also called class method.
Post Tags: Basic Java, Basic of Java, Basic of Java Programming, Java for Beginner, Java Programming, Learn Java
Related Posts
- More About Multithreading in Java
- Objects of Java
- Inheritance and Aggregation in Java Language
- The Basics of Multiple Threads in Java
- Basics of Java Programming
- Tomcat Performance Tuning
- The Modeling Approach Java Programs
- Concurrency—State Models & Java Programs
- Exception Handling Techniques in Java
- Top Software to Help Manage Your Websites
Popular
- iPhone or iPod Touch, Which One Should You Choose? - 47,761 views
- Introduction to Facebook - 26,656 views
- 7 Top Twitter Topic Trackers - 16,376 views
- Introduction of Hacking Methodology - 15,051 views
- Google Hacking Keywords - 11,718 views
- LG BD390 Wi-Fi Blu-ray Player Review: So Packed You’ll Forget About Blu [Review] - 10,094 views
- BlackBerry Curve 3G: A Familiar, Powerful Phone - 9,775 views
- Complete Guide to Web Site Marketing - 9,774 views
- Microsoft’s Zune HD Wiill Have OLED, HD Radio - 9,302 views
- Photo Album and Printing Services - 8,953 views
Featured Articles
- The Elements of the Facebook Platform
- Introducing the Facebook Platform
- Social Skills Today Are Being Lost
- GibBook – Gibraltar Own Social Network Site
- 7 Deadly Twitter Sins
- Introduction to Facebook
- Google’s Wave: Many Online Apps in One Tool
- App Mapping War Casualties Debuts for Memorial Day Weekend
- Google@Omgili Mashes Traditional Web Search With Social Buzz
- Capital Factory: Austin-Based Incubator (RWS Interview)
Recent Posts
- Tweet via something
- Lightroom Review And Other Phot
- Several Words About The Advantages Of Tablet PCs
- Dissimilar Positive Features Of Table Computers
- Digi Link Doctor
- Why You Need Professional SEO Software
- Automated SEO Tools For Online Business
- How To Choose The Best Tablet PC
- Tablet PC – The Latest Computer Technology
- Tablet PCs Compared To Laptops