24/01/25 ---------- Q)Java program on "private access modifier". class Student{ int rollno; String name; private int marks; private char grade; Student(int rollno,String name,int marks){ this.rollno=rollno; this.name=name; this.marks=marks; }//parameterised constructor void displayStudentDetails(){ System.out.println("ROLLNO:"+rollno); System.out.println("NAME:"+name); System.out.println("MARKS:"+marks); System.out.println("GRADE:"+grade); }//instance method void calculateGrade(){ if(marks>=80){ grade='A'; } else if (marks>=60 && marks<80) { grade='B'; } else{ grade='C'; } }//instance method } class StudentApplication{ public static void main(String[] args){ Student s=new Student(101,"Rama",76); s.calculateGrade(); s.displayStudentDetails(); } } Q)What is the output of the following program? class Employee{ int emp_count; Employee(){ emp_count++; } void displayEmpCount(){ System.out.println("Total Number of emps appointed:"+emp_count); } } class EmpCountApplication{ public static void main(String[] args){ Employee e1=new Employee(); e1.displayEmpCount(); Employee e2=new Employee(); e2.displayEmpCount(); } } Ans:- Total Number of emps appointed:1 Total Number of emps appointed:1 Note:- "Reason for the unexpected output is", the kind of variable used to keep track of total number of employees appointed. static modifier ---------------- Q) What is the purpose of "static" modifier? =>We can create 1)static variables 2)static methods 3)static initializer/block Q)What is a class variable? =>If "static" modifier is applied to an instance variable of a class, it becomes a (per) class variable.I.e. static variable is nothing but a class variable. Q)What are the differences between static variables and instance variables? instance variables static variables ---------------------------------------------------------------- 1)per object per class only one copy one seperate copy. irrespective of the number of objects. 2)meant for representing meant for representing individual object information. entire class level information. 3)contributes to the size doesn't contribute. of the object. 4)owned by an object. shared by every object of the class. 5)memory is allocated memory is allocated to the only when the object static variable as soon as is created. the class is loaded into memory and before any object is created. 6)memory is allocated memory is allocated in the method in the HEAP area. area. 7)using the reference can be referenced using (explicit/implicit reference) class name. only, instance variable can be referenced in the program.