Special Offer - Enroll Now and Get 2 Course at ₹25000/- Only Explore Now!

All Courses
Class and Object in Java

Class and Object in Java

April 30th, 2019

Class and Object in Java

Declaration

Class and Object in Java: Java Is a pure object oriented language. so java program contains only classes. each class has its own data items and functions. the data items are called fields and the functions are called methods.

Definition

A class is a collection of different properties, methods and blocks. We can include our own static blocks,constructors and inner class as well. It is a user defined data type and it contains data and its related methods.

Syntex

Data-type method-name (parameter list)
{
Body of the method
}
Program4
Class classname
{
Datatype field-1;
--------------------
Datatype field-n;
Datatype methodname-1(parameter list)
{
Body of the method
}
--------------------
Datatype methodname-n (parameter list)
{
body of the method
}
}
Each class have access scope to access their members by using ( .) (dot) operator.
package dkp.week3;public class ClassDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee e1= new Employee();
//Employee e2= new Employee("DKP","16-20-1990",15427.00);
e1.setValue("DKP", "19/5/2010", 104520);
e1.setName("Pandian");
e1.show();
System.out.println(e1.getName());
//e2.show();
Employee e2= new Employee("SURYA","16-20-1990",15400);
e2.setCheck(1005);
e2.show();
}
}
class Employee {
private String name;
private String doj;
private long salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDoj() {
return doj;
}
public void setDoj(String doj) {
this.doj = doj;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
public static int sample;
private int check;
public int getCheck() {
return check;
}
public void setCheck(int check) {
this.check = check;
}
Employee() {
System.out.println("Default Constructor is running");
name=null;
doj=null;
salary=0;
check=0;
}
Employee (String name,String d,long l) {
sample=sample+1;
check=check+1;
this.name=name;
this.doj=d;
this.salary=l;
}
public void setValue(String name,String d,long l) {
sample=sample+1;
check=check+1;
this.name=name;
this.doj=d;
this.salary=l;
}
public void show() {
System.out.println("Employee details");
System.out.println("Sample: "+sample);
System.out.println("check: "+check);
System.out.println(name+"\n"+doj+"\n"+salary);
}
}

Sample Output:

Default Constructor is running

Employee details

Sample: 1

check: 1

Pandian

19/5/2010

104520

Pandian

Employee details

Sample: 2

check: 1005

SURYA

16-20-1990

15400