Sunday, December 6, 2015

Introducation to Classes


Class in Java

A class is a group of objects that has common properties. It is a template or blueprint from which objects are created.  
 We have seen simple data types like int , float , double etc. If you want to create complex custom types, then we can make use of classes. A class consists of data and behavior. Class data is represented by it's fields and behavior is represented by its methods.
A class in java can contain:
  • data member
  • method
  • constructor
  • block
  • class and interface

Syntax to declare a class:

  1. class <class_name>{  
  2.     data member;  
  3.     method;  
  4. }  

Instance variable in Java

A variable that is created inside the class but outside the method, is known as instance variable.Instance variable doesn't get memory at compile time.It gets memory at runtime when object(instance) is created.That is why, it is known as instance variable.

Method in Java

In java, a method is like function i.e. used to expose behaviour of an object.

Advantage of Method

  • Code Reusability
  • Code Optimization
 

No comments:

Post a Comment