Class and Object

This section we are going to show the structure of the Class and Object and how to declare method, fields, and constructors.

The class is nothing but prototype (Blueprint) from which objects are created.

  • When we create a class in java the first step is keyword class and the name of the class or identifier we can say.
  • Next is a class body which starts with curly braces {} and between this, all things related to that class means their property and method will come here.

Sample example of a class given below.

Employee class doesn’t contain the main method. That’s a reason it’s not a complete application; it’s just the blueprint for Employee that might be used in an application. The responsibility of creating and using new Employee objects belongs to some other class in your application.

A class of java can contain:

  • Data member
  • Method
  • BLock
  • Constructor

Data member

A class can contain any of the following data members.

  • Local variables: variables are defined inside a method, block or constructor are called as a local variable. This variable is initialized inside a method and destroyed after complete execution of the method.
  • Instance variables: Variable defined outside a method and inside a class called as an Instance variable.
  • Class variables: Variable defined outside a method and inside a class with the static keyword.

Method

The method is like a function that is used to perform some operation in a class. In other words, a method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method’s name. Think of a method as a subprogram that acts on data and often returns a value.

Method syntax is given below :

A simple example of the method is given below :

Advantage of Method

  • Code Re-usability
  • Code Optimization

Block

In java mostly blocks are classified into two types these are static and non-static blocks

  • Static blocks: Static block mainly used for initializing the static data members. This block will be executed at the time of class loading in the memory. We can write multiple static block and execution of blocks done on the basic sequence. The syntax of the static block given below
  • The non-static block will call when a new instance is created and it will be called just before the Constructor. In case of multiple non-static blocks, the block executes the order in which it is defined in the class. There is no keyword prefix to make a block non-static block, unlike static blocks.

Constructor

The constructor is a special type of method that is used to initialize the object. All classes contain constructor. If a class has no constructor then JVM automatically supply a no-argument constructor.

For creating a constructor we need to follow two important rules:

  • Constructor name should be same as the class name
  • It does have an explicit return type

Object

The object is actual implantation of class prototype. In technically the main difference between class and object is a class is a template which describes the all the state and behavior of Instance and Object is actual implementation of that class which has some state & behavior in form of variable and methods and it acquires some memory space.

Class name refrenceobjectname = new Class name

Simple Example of object and class

There are many ways to create an object in java. They are:

  • By new keyword
  • By newInstance() method
  • By clone() method
  • By factory method etc

Related Posts:-

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons