×

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two types – static arrays and dynamic arrays. In this tutorial, we will restrict ourselves to a static array only.

We will begin our discussion with an understanding of the topic, its examples, the syntax for its declaration, and its advantages and disadvantage. Further, we will sum up our discussion with a java program.

Static Array

A static array is declared with the static keyword. Its nature is hinted at in the name itself. We cannot change or alter the static array. The memory is allocated at compile-time and its size also gets fixed therein.

Limitations in altering the size of static arrays led to the rise of the dynamic array where one can easily take the size as input at run time.

Example

Let’s consider an array named arr[20] which creates an array of size 20.

It means we can insertelements up to 20 only; we cannot insert a 21st element as the size of the Array is fixed.


int arr[] = { 81, 83, 84 , 88, 89 }; // static integer array  
int arr[] = { 32, 67, 44, 77 }; // static integer array  
int arr[] = { 542, 167, 414, 707, 55 }; // static integer array  


int* arr = new int[5]; // dynamic integer array  


Declaration of a Static Array

Let us look at the way to declare a static array in java.

<data type> <name of variables> []={<data1>,<data2>,<data3>, .....<dataN>};  

Here,

Data types – the type of data of the array

Name of variables – it consists of a list of variable names to be used.

{data1, data2, …..} – it consists of the list of data.

Examples

Let us look at the following examples to understand the ways to initialize the static array in java.

Example 1: By the aid of a new keyword


String[] states = new String[] {  
  "Arunachal Pradesh",   
  "Madhya Pradesh",   
  "Uttar Pradesh",   
  "Himachal Pradesh"    
};  


Example 2:

The array can also be declared in this way


String[] states = {  
  "Arunachal Pradesh",   
  "Madhya Pradesh",   
  "Uttar Pradesh",   
  "Himachal Pradesh"    
};  

Example 3:

This is another way to declare a static array. Here, it is being declared as a list.


List states = Arrays.asList(  
"Arunachal Pradesh",   
  "Madhya Pradesh",   
  "Uttar Pradesh",   
  "Himachal Pradesh"    
);  


Advantages of Static Array

  1. Its execution time is efficient.
  2. Statically allocated arrays remain alive throughout the complete runtime of the program.
  3. Global variables are declared “in advance of time,” such as a fixed array.
  4. The size of memory allocated to “data” is static. But it is possible to alter the content of a static structure without growing the memory space allocated to it.

Disadvantages of Static Array

  1. It often leads to the wastage of space if a great number of static data has been allocated in the beginning.
  2. It is impossible to increase the space once after allocating the memory initially. So, if one fails to allocate an adequate amount of space, it becomes problematic later on.

Implementation

Let us see a program to understand the topic clearly.

Example 1

public class StaticArrayExample1
{  
private static String[] arr;  
static   
{  
arr = new String[5];  
arr[0] = "India";  
arr[1] = "is";  
arr[2] = "a";
arr[3] = "developing";
arr[4] = "country";
}  
public static void main(String args[])   
{  
for(int i = 0; i<arr.length; i++)  
{  
System.out.print(arr[i] + " ");  
}  
}  
}  

Output

Static Array in Java

Description: This image shows the output of the code written above. Here, the keyword new has been taken into account to create a static array of type strings.

Example 2


public class StaticArrayExample2
{  
// A static array of integer type will be created
static Integer[] intArray;  
static   
{  
intArray = new Integer[] { new Integer(13), new Integer(42), new Integer(83), new Integer(49), new Integer(89)};  
}  
public static void main(String args[])   
{  
//for loop to iterate over static array      
for (int j = 0; j <intArray.length; j++)   
{  
//to print array elements      
System.out.println(intArray[j]);  
}  
}  
}  


Output

Static Array in Java

Description: This image shows the output of the code written above. Here, a static array of type integershas been created.

Summary:

In this tutorial, we understood what is meant by a static array with the help of certain examples. We saw the implementation to understand the topic even better.


Related Topics

Gregorian Calendar Java Current Date

GregorianCalendar class uses the Gregorian and Julian calendars. Dates are calculated by projecting present laws forever backward and forward in time. As a consequence, GregorianCalendar may be utilised to create...

8 minutes read.

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

How to Convert Decimal to Hexadecimal in Java

How to Convert Decimal to Hexadecimal in Java The hexadecimal number uses 16 values to represent a number. Numbers from 0 to 9 represented by digits and the numbers from 10...

3 minutes read.

Minimum Window Subsequence in Java

In this article, you will be very well acknowledged about the minimum window subsequence, what is the approach and how it is implemented. The example program is also executed and...

4 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

Java Enhanced For Loop (For-each Loop)

JAVA ENHANCED FOR LOOP The enhanced for loop is also called the for-each loop and has some advantages over the regular for loop. A for-each loop is designed to cycle through...

4 minutes read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.

Java Math decrementExact() Method

The decrementExact() method of Math class returns the argument which is decremented by one, throwing an exception is the result overflows an int or long. Syntax: public static int decrementExact (int a) Parameters: The...

1 minute read.

Properties Class in Java

Properties class is associated with Java since JDK 1.0, i.e. it is a legacy class. It is the subclass of Hashtable. It is used to maintain the lists of values in which...

5 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

11 minutes read.

Java Program to Print Permutations of String

A string is given and you need to print all the possible ways for that string. Permutation is arranging the characters of a string to get outputs from the given...

3 minutes read.

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.

StringBuffer in Java

StringBuffer in Java Similar to StringBuilder, the Java StringBuffer class is also used to create modifiable or mutable strings. The StringBuilder class is synchronized, i.e., thread-safe. Java StringBuffer ConstructorThe StringBuffer class has...

7 minutes read.

Methods in Java

The Methods in Java are the collection of statements that are executed when the method is called. By using the methods, the complexity of writing the code decreases. The method consists...

4 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.

Untouchable Number in Java

If a number N cannot be divided properly by any positive number, it is said to be an untouchable number. Additionally known as nonaliquot numbers. The sequence is A005114 from...

3 minutes read.