×

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

Java Logo

Java is a prominent and extensively used object-oriented programming language. In 1995, Sun Microsystems created it. Later in 2009, Oracle Corp takeover Java. History of Java Logo The name of the island...

3 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

How to create an array in Java

An array is a linear data structure stores a collection of fixed number ofelements of similar type. The size of an array is specified when it is created. The array...

14 minutes read.

How to convert double to String in Java

How to Convert double to String in Java It is used when we want to convert double primitive to String type. There are two methods to convert double to String. Using String.valueOf()...

2 minutes read.

Print Matrix Diagonally in Java

The aim is to print the elements of a matrix of size n*n in some kind of a diagonal pattern. Input : mat[3][3] = {{1, 2, 3},                      {4, 5, 6},                      {7,...

3 minutes read.

C# vs Java

Difference Between C# and Java C# and Java both languagesare popularly used programming languages. They both are derived from C/C++ programming and follow Object Oriented Programming approach. Even so, both these...

4 minutes read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

Internal Working of ArrayList in Java

Java's version of a resizable array is called ArrayList. The dynamic growth of an array list ensures that there is always room for new elements. ArrayList's backing data structure is...

8 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.

Dangling Else problem in Java

A language interpretation uncertainty is the hanging other issue. The following two types of condition executed code are both possible in programming: 1. if-then-else form 2. if-then form When dealing with the nested...

3 minutes read.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

New Features of Java 14

 Features like Switch expressions and text blocks which are previewed in Java 13 version are standardized in Java 14. Features in Java 14 Switch ExpressionText BlocksRecordsNull Pointer ExceptionsInstance ofPackaging ToolGarbage collectors 1.Switch Expressions Switch...

3 minutes read.

Awesome explanation of Strings in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java Base64 Encoding and Decoding

Introduction to Encoding and Decoding Encoding is the process of putting the sequence of characters like letters, numbers, punctuations, and other symbols into a specialised format for the efficient transmission or...

11 minutes read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

How to Iterate a HashMap in Java?

Hash-map is a class of Java collections framework which has the functionality to implement the Map interface of Java. It stores the data in key-value pairs and can be accessed...

5 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

Command Class in Java

We use the command class to run commands against the database. The Command class may find a set of parameter objects for use in sending values to a stored procedure...

3 minutes read.