Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Java Static keyword

The document explains the concepts of static variables, methods, and blocks in Java, highlighting their characteristics and usage. It also introduces the Vector class, detailing its properties, constructors, and methods, along with examples demonstrating their functionality. Additionally, it includes an assignment section with questions related to the discussed topics.

Uploaded by

Chaya Anu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Static keyword

The document explains the concepts of static variables, methods, and blocks in Java, highlighting their characteristics and usage. It also introduces the Vector class, detailing its properties, constructors, and methods, along with examples demonstrating their functionality. Additionally, it includes an assignment section with questions related to the discussed topics.

Uploaded by

Chaya Anu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Static Method, Variable and Block

What is Static Variable in Java?

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>

What is Static Method in Java?

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();
}
}

What is Static Block in Java?


The static block is a block of statement inside a Java class that will be executed when a class is
first loaded into the JVM. A static block helps to initialize the static data members, just like
constructors help to initialize instance members.

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);

/* Adding elements to a vector*/


vec.addElement("Apple");
vec.addElement("Orange");
vec.addElement("Mango");
vec.addElement("Fig");

/* check size and capacityIncrement*/


System.out.println("Size is: "+vec.size());
System.out.println("Default capacity increment is: "+vec.capacity());
vec.addElement("fruit1");
vec.addElement("fruit2");
vec.addElement("fruit3");
/*size and capacityIncrement after two insertions*/
System.out.println("Size after addition: "+vec.size());
System.out.println("Capacity after increment is: "+vec.capacity());
/*Display Vector elements*/
Enumeration en = vec.elements();
System.out.println("\nElements are:");
while(en.hasMoreElements())
System.out.print(en.nextElement() + " ");
}
}

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.

You might also like