by Dinesh Thakur Category: Classes

Classes are the fundamental building blocks of any object-oriented language.

A class describes the data and behaviour associated with instances of that class. When a class is instantiated, an object is created: this object has properties and behaviour similar to other instances of the same class. The data associated with a class or object is stored in variables. The behaviour associated with a class or object is implemented by means of methods. Methods are similar to the functions or procedures of procedural languages such as C or Pascal.

A class can be defined as follows:

class First { // Body of the class }

The keyword class is used to define a class. The keyword is followed by the name of the class (in the above example First). The body of the class is contained within curly brackets. The body consists of statements related to constructors, methods and variables.

Every class defined in Java is a child of the Object class.

A class may have any of these variable types:  
Local: defined within a constructor, block or method. The variable is declared and then initialised inside a method and destroyed once the method has ended.  
Instance: defined in a class but are outside a method. Initialised when instantiation of the class happens and can access from inside any constructor, block or method of the class.  
Class: declared inside the class, uses the static keyword and are outside any method. 

Creating instance and class variables

A class usually contains variables and methods. Certain specific rules have to be followed for defining variables and methods within a class. This section deals with the definition of different types of variable, namely instance variables, constants and class variables.

Instance variables

Instance variables are declared and defined in almost the same way as local variables, the main difference being their location in the class definition. Variables are considered instance variables. if they are declared outside a method definition. It is customary to define instance variables just after the first line of the class definition.

Constants

A constant variable or constant is a variable whose value never changes.

Constants are used to define shared values for all the methods of an object so that object-wide values that will never change can be given meaningful names. In Java, only instance and class variables can be constants (not local variables). To declare a constant, the keyword final is used. It should be placed before declaration of the variable and should include an initial value for that variable, as shown in the following examples:

final float pi = 3.14:

final boolean debug = false;

final int maxsize = 40000;

Constants can be useful for naming various states of an object and testing them. For a test label that can be aligned left, right or centre, the values can be defined as constant integers.

final int LEFT = 0;

final int RIGHT = 1;

final int CENTER= 2;

Class variables

Class variables are global to a class and to all instances of that class. Class variables are used for communication between different objects within the same class. These variables are also used for tracking global states among a set of objects. The static keyword is used in the class declaration to declare a class variable. Some examples of this are given below:

static int sum;

static final int maxObjects = 10;

Constructors 

Every class have a constructor; if you omit it, a default one built by the compiler. When you create a new object, at least one constructor must invoke.  As a rule, the name of a constructor must be the same as that of the class, and there can be as many constructors as a class requires.  It is what a constructor looks like: 

publicclass Puppy {
    public
Puppy(){}
    public Puppy(String name){
          // This constructor has a single parameter, name.
    }

}

Creating Objects

We already know that a class is a kind of blueprint to create objects from so the object is created from the class. For a new object to be cre-  ated, we need the new keyword.  
These are the three steps needed to create an object from a class:  
● Declaration:  a variable must declare with a name and the object type  
● Instantiation: the new keyword use for creating the object.
● Initialisation: the constructor called, and this initialises the object.  
The next example shows how objects create:

public class Puppy {
     public
Puppy(String name) {
           // This constructor contains a single parameter called name.
           System.out.println("Passed Name is :" + name );

     }
    public static void main(String []args) {
         // The next statement create an object called myPuppy
         Puppy myPuppy = new Puppy( "fluffy" );
     }
}



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles