Java Static keyword
Java Static keyword
Static variable in Java is variable which belongs to the class and initialized only once at the start of
the execution. It is a variable which belongs to the class and not to object(instance). A single copy
to be shared by all instances of the class
A static variable can be accessed directly by the class name and doesn’t need any object
Syntax :
<class-name>.<variable-name>
Static method in Java is a method which belongs to the class and not to the object. A static method
can access only static data. It cannot access non-static data (instance variables).
A static method can call only other static methods and can not call a non-static method
from it.
A static method can be accessed directly by the class name and doesn’t need any object
A static method cannot refer to "this" or "super" keywords in anyway
Syntax :
<class-name>.<method-name>
Note: main method is static, since it must be accessible for an application to run, before any
instantiation takes place.
Example
class Student {
int a; //initialized to zero
static int b; //initialized to zero only when class is loaded not for each object created.
Student(){
//Constructor incrementing static variable b
b++;
}
public void showData(){
System.out.println("Value of a = "+a);
System.out.println("Value of b = "+b);
}
}
public class Demo{
public static void main(String args[]){
Student s1 = new Student();
s1.showData();
Student s2 = new Student();
s2.showData();
}
}
class Test{
static {
//Code goes here
}
}
Example
public class Demo {
static int a=80;
static int b=100;
static {
a = 10;
b = 20;
}
public static void main(String args[]) {
System.out.println("Value of a = " + a);
System.out.println("Value of b = " + b);
}
}
Vector Class
Vector implements a dynamic array. It is similar to ArrayList, but with two differences:
Vector is synchronized.
Vector contains many legacy methods that are not part of the collections framework.
Vector proves to be very useful if you don't know the size of the array in advance or you just need
one that can change sizes over the lifetime of a program.
Below given are the list of constructors provided by the vector class
Vector vec = new Vector();
o It creates an empty Vector with the default initial capacity of 10.
o It means the Vector will be re-sized when the 11th elements needs to be inserted
into the Vector. Note: By default vector doubles its size. i.e. In this case the Vector
size would remain 10 till 10 insertions and once we try to insert the 11th element It
would become 20 (double of default capacity 10).
Vector object= new Vector(int initialCapacity)
o Vector vec = new Vector(3);
o It will create a Vector of initial capacity of 3.
Vector object= new vector(int initialcapacity, capacityIncrement)
o Vector vec= new Vector(4, 6)
o Here we have provided two arguments. The initial capacity is 4 and
capacityIncrement is 6. It means upon insertion of 5th element the size would be 10
(4+6) and on 11th insertion it would be 16(10+6).
Methods in Vector Class
1. void addElement(Object element): It inserts the element at the end of the Vector.
2. int capacity(): This method returns the current capacity of the vector.
3. int size(): It returns the current size of the vector.
4. void setSize(int size): It changes the existing size with the specified size.
5. boolean contains(Object element): This method checks whether the specified element is
present in the Vector. If the element is been found it returns true else false.
6. boolean containsAll(Collection c): It returns true if all the elements of collection c are
present in the Vector.
7. Object elementAt(int index): It returns the element present at the specified location in
Vector.
8. Object firstElement(): It is used for getting the first element of the vector.
9. Object lastElement(): Returns the last element of the array.
10. Object get(int index): Returns the element at the specified index.
11. boolean isEmpty(): This method returns true if Vector doesn’t have any element.
12. boolean removeElement(Object element): Removes the specifed element from vector.
13. boolean removeAll(Collection c): It Removes all those elements from vector which are
present in the Collection c.
14. void setElementAt(Object element, int index): It updates the element of specifed index with
the given element.
Example
import java.util.*;
public class VectorExample {
public static void main(String args[]) {
/* Vector of initial capacity(size) of 2 */
Vector<String> vec = new Vector<String>(2);
Assignment
1. What is the difference between static and instance variables? Explain with an example
2. Why static variables are not available to static methods?
3. What are static methods? Explain the importance of static method with an example.
4. Write a program to demonstrate vector class.
5. Differentiate between array and vector.
6. Differentiate between size and capacity in vector class. Explain with a program.