×

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

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

Java Session

Session indicates interval of time. A session is a simple  time interval in which servers and client interacts. To maintain the state of the client or user we use technologies...

5 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

Java Pass-by-Reference

In Java, passing parameters can be done using one of two fundamental methods. Pass-by-value is used for the first and pass-by-reference is used for the second. One thing to keep...

2 minutes read.

Automatic Resource Management

In computer programming, resource management refers to the wide set of techniques for the effective management of the system resources. There are two ways of management of resources: The computer program itself...

2 minutes read.

How to install Java in Windows 10

To make programs that can run on our systems, we need to install the programming language related software in our systems. Different programming language requires a different type of software aka...

6 minutes read.

How to download and install Eclipse in Windows?

Download and Install Eclipse on Windows Eclipse is an open source IDE (Integrated Development Environment) which is used to help the programmers to provide a platform to write and run the...

1 minute read.

Hello Program in Java

Hello Program in Java The Hello program in Java displays the word Hello on the console. It is the basic program in Java. In this section, we will learn different ways...

3 minutes read.

Interface in Java

  Interface in Java In Java, the interface is just like a class that has only static constants and abstract methods. It is used to achieve polymorphism so that it can also...

6 minutes read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Difference between print() and println() in Java

In this tutorial, we will discuss the differences between print and println in Java language. There are mainly two methods to display the text on the console printprintln The above two methods are...

4 minutes read.

Java Queue Interface

Queue interface is a subtype of Collection interface. All methods in the Collection interface are also available in the Queue interface. It provides operations of Collection and also some additional...

2 minutes read.

Java Integer compareUnsigned() method

The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned. Syntax public static int compareUnsigned(int x , int y) Parameters The parameters ‘x’ and ‘y’ represent the...

1 minute read.

How to set timer in Java

In this article, you will be very well equipped with the knowledge to set timer in java. The timer in java can be set by using timer class provided by...

3 minutes read.

Various operations on HashSet in Java

In this article, you will be acknowledged about what is a HashSet in java and what are its operations in java programming language. The HashSet is a crucial part of...

3 minutes read.

Design of JDBC

Java applications may interface using database systems from many vendors using the Java Database Connectivity (JDBC) Application Software Interface (API) from Sun Microsystem. To connect spreadsheets, JDBC and database drivers...

3 minutes read.

Java Math floorMod() Method

The floorMod() method of Math class returns the floor modulus of the specified arguments. It firstly divides the dividend and divisor and then returns an integer that is equal to...

1 minute read.

Java Generate UUID

What java Generate UUID? A 128-bit long value that is universally unique serves as the basis for the terms UUID (Universally Unique Identifier) and GUID (Globally Unique Identifier). Hexadecimal (octet) digits...

5 minutes read.

Java vs JavaScript

Java Vs JavaScript Java and JavaScript, both play an important role in the field of Computer Technology. Most people think JavaScript is a part of Java. But it is not entirely...

4 minutes read.