• A Class is a container that combines data members and method within a single unit knows as class
  • It is a template for creating new User defined data type
  • It defines a structure for storing both data and methods
  • It can be used to create objects (instance of a class) of class type

Syntax:

class ClassName
{
		dataType instanceVariable1;
		dataType instanceVariable1;
		
                returnType methodName1 (parameter list)
		{
			Method body;
		}
		returnType methodName2 (parameter list)
		{
			Method body;
		}
}
  • A class is declared with the class keyword
  • data or variable are called instance variable.
  • The method and variables defined within a class are called class members.
  • Method contains code that can be operate on data

Example:

class student
{
	introllNo,total;
	String name;
}
class studentDemo
{
	public static void main (String args[])
        {
		studentstdObj= new student();
		doubleper;
		stdObj.rollNo =1;
		stdObj.name = “abc”;
		stdObj.total = 450;
		per = stdObj.total/6;
		System.out.println (“Persantageis : “+per);
	}
}
  • Each object has a separate copy of the data members
  • The dot (.) operator is used to access instance members of the class.

Declaring Object

  • studentstdObj; // declare ref. to object containing null value.
  • stdObj = new student (); //Actualmemory is allocatedto student object.
  • General form of a new operator is as follow:
classVar = newclassName ();
stdObj = newstudent ();
  • Where, classVar a class type variable and className is the name of the class.
  • The classname followed by parentheses specifies the constructor for the class.
  • A constructor initializes instance member when object of a class is created.
  • Most classes explicitly define their own constructors within their class definition.
  • Default constructor will be supply automatically if no explicit constructor is specified.

Assigning Object Reference Variable

Box b1 = new Box ();
Box b2 = b1;

  • The above statement assigns reference of b1object to b2 and both will point to the same object.
  • It did not allocate any memory or copy of the original object.
  • It simply assigns reference of the b1 object to the b2 object and any changes made to the object b2 will also affect the b1 because both points to the same object.

Introducing Methods

  • A method is a block of code that performs a specific task
  • It has the following form:

returnType methodName (parameter-list)
{
	Body of the Method ..
	[return value]
}

  • Where, returnType Specifies the data type returned by the method. If the method does not return a value, its must be declared as void.
  • methodName specifies method name.
  • Parameter-list specifies list of parameters with type and identifiers separated by commas.
  • Parameters are variables that receive the value of the argument passed to the method when it is called.
    The parameter list may be empty if method has no parameters.
  • Methods having return type other than void return a value to the calling routine using the form [return value].

Types of Methods

  1. Does not return value – void
  2. Returning a value
  3. Method which takes parameter

Does not return value – void

	voidsquare()
	{
		System.out.println(“Square is:“+ x*x);
	}

Returning a value

	intsquare()
	{
		returnx*x;
	}

Method which takes parameter

int square (int x)
{
		returnx * x;	
}
int x;
		x = square (5);

  • It will return the square of passed value to the square method as a parameter.
  • A parameter is a variable defined by a method that receives a value when the method is called. For example in square (), x is a parameter.
  • An argument is a value which is passed to a method when it is called. For example statement square (5) passes value 5 as argument.

Garbage Collection

  • Objects are dynamically allocated by using the new operator.
  • In some languages, such as C++, memory allocated to objects must be manually released using delete operator.
  • Java handles de-allocation of the memory automatically garbage collection technique.
  • When no reference to an object is available than such object is marked for garbage collection and the memory allocated to that object can be freed.
  • Memory management can be a difficult task in traditional programming environments.
  • For example, in C/C++, the programmer must manually allocate and free all dynamic memory.
  • Sometimes it may create a problem, because programmers will either forget to free memory that has been previously allocated or try to free some memory that another part of their code is still using.
  • Java solves this problems by managing memory allocation and de-allocation automatically using garbage collection technique.

Finalize() Method

  • Sometimes will need to perform some action when object is destroyed.
  • For example, releasing some resource such as a file before an object is destroyed.
  • To handle such situations, java provides a mechanism called finalization.
  • Finalization method allows to perform some specific actions when an object is about to destroy.
  • The concept of finalize method is similar to the concept of destructor.
  • The java run time calls the finalize method whenever an object of that class is about to destroy.
Protected void finalize ()
{
	// finalize code
}
  • The finalize() is called before the garbage collection performs.
  • It is not called when an object goes out-of-scope.
  • We cannot know whether finalize() will be executed so program must provide other approach for releasing system resources used by the object.
  • It must not depend on finalize() for shut down activities.