Sie sind auf Seite 1von 3

JAVA CLASSES

Before learning what classes are ? Lets us learn about objects first. Objects are the key
to understanding object-oriented technology. An object is made up of states and
behaviors which is used as model for objects in the real world. Some of the real world
objects are dogs, bicycles, television sets. Dogs have state like name, breed, color etc
and behavior like barking, fetching etc. The object's behavior is defined by its methods
and state is represented by its instance variables.

OBJECT

An objects represents one particular thing and class represents a category of things.

The class is at the core of Java. Let us see some basic elements of class and how classes
are used to instantiate objects. Every class defines a new data type. This new type can be
used to create objects of that type. The general form of class is as :

class classname{
type instance-variable1;
type instance-variable2;
//......
type instance-variableN;

type methodName1(parameter-list){
// body of method;
}

type methodName2(parameter-list){
// body of method;
}

//......
type methodNameN(parameter-list){
// body of method;
}
}

The variables defined in the class are called instance variables and code is called
methods. The methods and instance variables collectively are called members of class.
Generally instance variable are accessed and modified by methods defined in the class.

Let us demonstrate the classes with a simple program.

class Employee
{
String firstName;
String lastName;
double salary;
}

Here class Employee defines three instance variables : firstName, lastName and salary.
Employee class is a new data type. It must be kept in mind that a class declaration
creates a template and not an actual object. To create an actual object of Employee class,
you will see the statement like below :

Employee ob = new Employee();

ob is an instance of class Employee. Every instance of class has its own copy of instance
variables. Thus every instance of class Employee will have its own copy of firstName,
lastName and salary. Thus every object you create have its own identity I.e all objects
are distinct. To access these variables you will use dot(.) operator as show in the
statement below :

ob.firstName = “John”;

Here is the complete program that uses the above Employee class.

class Employee
{
String firstName;
String lastName;
double salary;
}
class EmployeeDemo{
public static void main(String args[]){
Employee ob = new Employee();
String fullName;

// assigns values to the ob's instance variables


ob.firstName = “John”;
ob.lastName = “ Crully”;

// computes full name of the employee


fullName = firstName + lastName;
System.out.println(“The full name of employee is ”+fullName);
}
}

The above program should be placed in EmployeeDemo.java because the main method
is in EmployeeDemo class and not in Employee class. When we compile this program
two .class files will be created, one for Employee class and other for EmployeeDemo
class. The java compiler automatically puts each class into its own .class file.

The output produced by above program will be :

The full name of employee is John Crully

NEW OPERATOR

The new operator dynamically allocates memory for an object. It has general form as :

class-variable = new classname();

here class-variable is object of class type being created. The classname is the name of
class being instantiated. The classname followed by parenthesis specified the constructor
of the class. A constructor is used to initialize an object of class when the object is
created. If no explicit constructor is specified then java will automatically supply a
default constructor. It must be noted that new keyword supplies memory to object during
runtime. Thus from above discussion it is clear that a class is a logical construct and an
object has physical reality.

Das könnte Ihnen auch gefallen