Thursday 25 June 2015

Static and Non-static program

NonStatic program
package Javaprgs;

public class Nonstaticmethod {
void addition(){
        int a = 4;
        int b = 5;
        System.out.println(a+b);

}

public static void main(String[] args) {
//nonstatic method we have to create object

Nonstaticmethod ns=new Nonstaticmethod();
ns.addition();/ /with  object refrence we  cal nonstatic method
}

}



Static Program

package Javaprgs;

public class Staticmethod {
public static void main(String[] args){
        System.out.println("Program starts here.");//first line
        addition();  //this is calling the addition() method
        System.out.println("Program ends here.");
}
static void addition(){
        int a = 4;
        int b = 5;
        System.out.println(a+b);

}

}


No comments:

Post a Comment