Java session5
-
Upload
jigarthacker -
Category
Education
-
view
588 -
download
0
Transcript of Java session5
Internet technologiesJigar Jobanputra
Defining a class in java•class rectangle
{
}
Various members within classclass student{
public static void main(String[] args){
student s1,s2;}
}
Creating a instance class student{
public static void main(String[] args){
student s1,s2;s1=new student();s2=new student();
}}
Instance variableclass student{
int rollno,enrollno;}
Accessing instance variableclass student{
int rollno,enrollno;public static void main(String args[]){student s1;s1=new student();s1.roll=1;s1.enroll=1111;}
}
methodclass 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());}
}
Practice example for class•Prepare bank class•Data members : acno,Name,balance•Member function:
▫View balance▫Deposite▫withdraw
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{}}