6. Accessing instance variable
class student
{
int rollno,enrollno;
public static void main(String args[])
{
student s1;
s1=new student();
s1.roll=1;
s1.enroll=1111;
}
}
7. method
class student
{
int roll,enroll,m1,m2,m3;
int total_marks()
{
return(m1+m2+m3);
}
public static void main(String [] args)
{
student s1;
s1=new student();
s1.m1=10,s1.m2=20,s1.m3=30;
System.out.println(“Total marks is :” + s1.total_marks());
}
}
8. Practice example for class
• Prepare bank class
• Data members : acno,Name,balance
• Member function:
▫ View balance
▫ Deposite
▫ withdraw
9. Constructor
• We define constructor by using the class name. it is
written almost similar to a method, but it should not
have a return type and its name is the same as the class
name.
class student
{
student() //default constructor
{
}
student(int a) //parameterized constructor
{
}
}