Showing posts with label Definitions. Show all posts
Showing posts with label Definitions. Show all posts

Thursday, 15 March 2012

Array Concept in java

Array Definition:
        Array is an variable that holds multiple values of same data type.

How to get array length in java? 
array length Definition: 
arr.length give the length of the array where arr is the array name.
Syntax:
arrayname.length;


Array simple Program: 
package array_pack;
class arr
{
      private int arr[] = new int[2];
      public void array_init()
      {
      arr[0] = 1;
      arr[1] = 2;
      }
      public void array_print()
      {
      for(int i = 0; i<arr.length; i++)
      System.out.println(arr[i]);
      }
}
public class arr_process {
      public static void main (String args[])
      {
            arr a = new arr();
            a.array_init();
            a.array_print();
      }
}

Output: 
1
2

Explanation: 
           First the program creates the instance for the class arr..Now with the help of the instance we are calling the array_init() function and array_print() function.

Note :
arr.length give the length of the array where arr is the array name.

Tags:
Array Declaration,Array,Array in java,A simple array program,array definition,array concept in java,programs,simple programs,java beginners,array length,

Static Concepts in java

Static Definition:
        In classes, a static method is one that is called without an instance of that class(object), while a non-static method is called by instances of the class(object).  

Static Program: 
package static_pack;
public class stat {
      static int i = 12;
      int j = 20;
      static
      {
            subclass sub = new subclass();
            System.out.println("The Static value is: " +i);
            //System.out.println("The Static value is: " +j);
           
            sub.display();
      }
      public static void main(String[] args)
      {
            System.out.println("Oops Main class after static block...");
            subclass.print();
      }
}
create a subclass class in the same package:

package static_pack;
public class subclass
{
      static void print()
      {
            System.out.println("In Subclass static method");
      }
      public void display()
      {
            System.out.println("Static yaar!!");
      }
}

Output:-
The Static value is: 12
Static yaar!!
Oops Main class after static block...
In Subclass static method

Explanation :
                 From the above program u can understand that Static block gets executed before the public static void main(String args[]). The static block gets executed and then the main block gets executed

Tags:
        Static program in java, Static program with Example, Static concept java, simple Static program,interview static,static program,use of static block,static block,Static function,Static Declaration