Sunday, 12 February 2017

JAVA PROGRAM USING ENCAPSULATION TECHNIQUE

// JAVA PROGRAM USING ENCAPSULATION TECHNIQUE

class Student{
int rollno;
double marks;
String name;

void display(){
System.out.println("rollno:"+ rollno);
System.out.println("marks:"+ marks);
System.out.println("name:"+ name);
}

public static void main(String[] ar){
System.out.println("student information");
Student st=new Student();
st.display();
}
}

OUTPUT;
rollno:0
marks:0.0
name:null




when the above java program Student.java is compiled, the compiler will verify whether the java code available in the program is valid or not, if valid the compiler will generate the .class file. The .class file generated by the compiler will be provided to the JVM for execution.

The execution of the java program will be done by the JVM and it begins from the main(). Initially the java stack will be empty. the JVM will begin execution of the program by calling main(). When a method is called for execution then that method will be pushed into the java stack and then begins the execution.

Student st= new Student();

The above statement will create an object of Student class. Creating an object means allocating memory for the instance variables in heap memory.

A class can contain any number of methods, but the JVM can call only main() for execution. If we want other methods to be executed, then it is the responsibility of the programmer to call the other method which has to be executed.

st.display();

The above statement will call display() for execution. When the display() is called for execution, the display() will be pushed into the java stack and then begins the execution. When a method is completely executed then that method will be popped(removed) from the java stack.



TO KNOW ABOUT OOPS CONCEPTS CHECK HERE........



No comments:

Post a Comment