Why is the main method static in java?

To understand this first you need to know why do we create static methods and how do we call them learn about static methods at :

Main method is declared static because static methods do not need any instance to get themselves called. Java is Object Oriented language so it is necessary to declare the  main method as static because their are some conditions in which we don’t want to create the object for example if we want to do simple programming without using Concepts of OOP then their is no way to call the main method but by declaring it static. The code below will help you understand.

  public class MyClass{

public void main(String arguments[]){

System.out.println(“codeheaven.wordpress.com”);

}

}

The above code generates an error: main method is not static please define the main method as:
   public static void main(String[] args)
But when the main is made static this code runs fine and gives the output: codeheaven.wordpress.com

          If you find my answer intresting and easy to understand invite your friends to read this and Don’t Forget to Like and give your feedback and suggesions 🙂

Advertisements

Array Reversing Program in Java

public class ArrayReversing {
public static void main(String a[]){
int arr[]={ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8  };
int temp;
int as = arr.length;
int k = as – 1;

System.out.println(“Array Before Reversing”);
printArray(arr);//method used to print array on screen

ArrayReverse://using loops with title
for(int i = 0; i < arr.length/2 ; i++){
temp = arr[k];// swaping
arr[k] = arr[i];
arr[i] = temp;

k–;
}
System.out.println(“Array After Reversing”);
printArray(arr); // calling the method printArray to print the elements of array
}

static void printArray(int ar[]){
PrintArray:
for(int l:ar)
System.out.println(l);
}

}

Output:

Array Before Reversing
1
2
3
4
5
6
7
8
Array After Reversing
8
7
6
5
4
3
2
1

Running Java Code from Command Prompt

To run java you must install JDK. If you dont have it download it from :

http://www.oracle.com/technetwork/java/javase/downloads/

 

Setting Enviroment Variables:

     Right Click Computer

     Click Advanced System Settings under properties

     Click New  under Enviroment Variables

     Set “path” as Variable name

     For Variable value you need to go to the folder where you installed JDK

     Inside JDK installation directory open subdirectory bin

     Right Click any file inside that directory Under properties

     Copy the address of the file that address is Actually the Variable value of variable path press “ok”

   Check:

           Open Command Prompt  and type  “javac” alot of crap will show up. This is the indication that you’re ready to go!

           In Command Prompt change your current directory to the directory in which u saved the file which u want to run

           type “java FileName” ‘java ‘ is the command to execute the java code “FileName” is the name of .class file.

                                                               🙂     ENJOY!!!!!