Web
Analytics
Java Classes and Objects | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

Classes and Objects

Class Definition

—A class is  prescription for a particular kind of object.

—Objects can be created using the class.Object can access all the member variables and methods(Depends on the acess modifiers)

—A class have two things-

1.Fields :- Variables that store class data items and different for each class object.Such variables are known as instance variables if we declare any variable as static(also known as class level variables) then it will be shared among all the class objects.Class level variables can be used by class name or by class object.

2.Methods:-Methods define the behaviour of the class .Generally used to set and get the values of member variables.

Example: 

Money Class(Money.Java) 

public class Money {
private int rs;
private int paisa;
public String name;
public static int count=0;

public Money()
{
	this.rs=0;
	this.paisa=0;
	this.name="";
	count++;
}
public Money(int rs, int paisa, String name)
{
	this.rs=rs;
this.paisa=paisa;
this.name=name;
count++;
}
public Money(Money m)
{
	this.rs=m.rs;
	this.paisa=m.paisa;
	this.name=m.name;
	count++;
}
public String Show()
{
	return this.rs+" "+this.paisa;
}
}

Main Class

public class Mainclass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Counter Value is "+Money.count);
		Money m1=new Money();
		Money m2=new Money();
		System.out.println("Counter Value after object creation is "+Money.count);
		Money m3=new Money(12,23,"Yogeshdotnet");
		System.out.println("Counter Value after object creation is "+m3.Show());
		Money m4=new Money(m3);
		System.out.println("Counter Value after object creation is "+m3.Show());
	}

}

Download Source Code

Youtube Video