Class and Object

To create an object in Java, you need a class. A class encapsulates the instance variables and methods that define an object.
You interact with a Java object by invoking the object's instance methods which have executable code that manipulates the object's instance variables.

Defining Class and creating object:

//Defining Class
class  Students
{
    //Instance Variable
	long regno;
	String name;
	String Course;
	int sem;
	
	// Instance Method
  	
	void SetValue(long regno,String name,String Course,int sem)
	{
		this.regno= regno;
		this.name=name;
		this.Course=Course;
		this.sem=sem;
	}
	
	
	void ShowValue()
	{
		System.out.printf("\nRegistration  Number::%d", this.regno);
		System.out.printf("\nStudent  Name::%s", this.name);
		System.out.printf("\nCourse::%s", this.Course);
		System.out.printf("\nSemester::%d", this.sem);
	
	}	
}
public class soft {
	
	public  static void main(String  args[])
	{
	// Creating Object
		Students  s= new  Students();
		s.SetValue(1001,"Abhinav","M.Tech",2);
		s.ShowValue();
	}
}

output

Registration Number::1001

Student Name::Abhinav

Course::M.Tech

Semester::2

Following class contain a default constructor that initialize the instance variable!
Example:

declare a class for bank customer that can perform three (action)methods ( Depositing Money,Drawing Money and asking for Balance).

//Defining Class with Defualt constructor

class  bankCustomer
{
  private  int accno;
  private  String  name;
  private String  address;
  private  double  balance;
  public bankCustomer()
  {
  balance=0;
  }  
  public void  AccountOpen(int acno,String name,String address)
  {   
    this.accno=acno;
    this.name=name;
    this.address=address;    
  }  
  public void  deposit (double amt)
  {
      System.out.println("Action::Depositing Money Rs: "+amt);
	  balance=balance+amt;	  
  }

  public void  draw (double amt)
  {
	  System.out.println("Action::Drawing Money Rs: "+amt);
	  if( (balance-amt)<=500)
	  {
	  System.out.println("Minimum balance restriction voilated!");
	  }
	  else
	  {
	  balance=balance-amt;
	  }
  }	
  public void  getbalance ()
  {	  
	  System.out.println("Account Holder name::"+name);
	  System.out.println("Account Holder Address::"+address);
	  System.out.println("Your Account Balance::"+balance);
	  System.out.println("----------------------------------------");
  }
}
public class soft {
	
	public  static void main(String  args[])
	{
		bankCustomer  a= new  bankCustomer();		
		a.AccountOpen(9081, "Madhuraj","SugarNagar" );
		a.getbalance();	
		
		a.deposit(1000)	;
		a.getbalance();
		
		a.draw(300);
		a.getbalance();	
	}
}

Output

list1