Você está na página 1de 2

Array: Array is the most important thing in any programming language. By definition, array is the static memory allocation.

It allocates the memory for the same data type in sequence. It contains multiple values of same types. It also store the values in memory at the fixed size. Multiple types of arrays are used in any programming language such as: one - dimensional, two - dimensional or can say multi - dimensional. Declaration of an array: int num[]; or int num = new int[2]; Some times user declares an array and it's size simultaneously. You may or may not be define the size in the declaration time. such as: int num[] = {50,20,45,82,25,63}; In this program we will see how to declare and implementation. This program illustrates that the array working way. This program takes the numbers present in the num[] array in unordered list and prints numbers in ascending order. In this program the sort() function of the java.util.*; package is using to sort all the numbers present in the num[] array. The Arrays.sort() automatically sorts the list of number in ascending order by default. This function held the argument which is the array name num.

import java.util.*; public class array{ public static void main(String[] args){ int num[] = {50,20,45,82,25,63}; int l = num.length; int i,j,t; System.out.print("Given number : "); for (i = 0;i < l;i++ ){ System.out.print(" " + num[i]); } System.out.println("\n"); System.out.print("Accending order number : "); Arrays.sort(num); for(i = 0;i < l;i++){ System.out.print(" " + num[i]); } } }

This Java programming example will teach you the way to define a static methods. In java we have two types of methods, instance methods and static methods. Static methods can't use any instance variables. The this keyword can't be used in a static methods. You can find it difficult to understand when to use a static method and when not to use. If you have a better understanding of the instance methods and static methods then you can know where to use instance method and static method.

A static method can be accessed without creating an instance of the class. If you try to use a non-static method and variable defined in this class then the compiler will say that non-static variable or method cannot be referenced from a static context. Static method can call only other static methods and static variables defined in the class. The concept of static method will get more clear after this program. First of all create a class HowToAccessStaticMethod. Now define two variables in it, one is instance variable and other is class variable. Make one static method named staticMethod() and second named as nonStaticMethod(). Now try to call both the method without constructing a object of the class. You will find that only static method can be called this way. The code of the program is given below:
public class HowToAccessStaticMethod{ int i; static int j; public static void staticMethod(){ System.out.println("you can access a static method this way"); } public void nonStaticMethod(){ i=100; j=1000; System.out.println("Don't try to access a non static method"); } public static void main(String[] args) { //i=100; j=1000; //nonStaticMethod(); staticMethod(); } }

Você também pode gostar