What is Classes and Objects in Java

The Java programming language, like many object-oriented programming languages, provides a tool to solve programming problems using the notions of classes and objects. Every object has a class that defines its data and behavior. Each class has three kinds of members:

Fields are data variables associated with a class and its objects. Fields store results of computations performed by the class.

Methods contain the executable code of a class. Methods are built from statements. The way in which methods are invoked, and the statements contained within those methods, are what ultimately directs program execution.

Classes and interfaces can be members of other classes or interfaces.

Here is the declaration of a simple class that might represent a point on a two-dimensional plane:

class Point
{
public double x, y;
}



This Point class has two fields representing the x and y coordinates of a point and has (as yet) no methods. A class declaration like this one is, conceptually, a plan that defines what objects manufactured from that class look like, plus sets of instructions that define the behavior of those objects.

Members of a class can have various levels of visibility or accessibility. The public declaration of x and y in the Point class means that any code with access to a Point object can read and modify those fields. Other levels of accessibility limit member access to code in the class itself or to other related classes.

Creating Objects
Objects are created by expressions containing the new keyword. Creating an object from a class definition is also known as instantiation; thus, objects are often called instances.

Newly created objects are allocated within an area of system memory known as the heap. All objects are accessed via object referencesany variable that may appear to hold an object actually contains a reference to that object. The types of such variables are known as reference types, in contrast to the primitive types whose variables hold values of that type. Object references are null when they do not reference any object.

Most of the time, you can be imprecise in the distinction between actual objects and references to objects. You can say, "Pass the object to the method" when you really mean "Pass an object reference to the method." We are careful about this distinction only when it makes a difference. Most of the time, you can use "object" and "object reference" interchangeably.

In the Point class, suppose you are building a graphics application in which you need to track lots of points. You represent each point by its own concrete Point object. Here is how you might create and initialize Point objects:

Point lowerLeft = new Point();
Point upperRight = new Point();
Point middlePoint = new Point();

lowerLeft.x = 0.0;
lowerLeft.y = 0.0;

upperRight.x = 1280.0;
upperRight.y = 1024.0;

middlePoint.x = 640.0;
middlePoint.y = 512.0;



Each Point object is unique and has its own copy of the x and y fields. Changing x in the object lowerLeft, for example, does not affect the value of x in the object upperRight. The fields in objects are known as instance variables, because there is a unique copy of the field in each object (instance) of the class.

When you use new to create an object, a special piece of code, known as a constructor, is invoked to perform any initialization the object might need. A constructor has the same name as the class that it constructs and is similar to a method, including being able to accept arguments. If you don't declare a constructor in your class, the compiler creates one for you that takes no arguments and does nothing. When we say "newPoint() " we're asking that a Point object be allocated and because we passed in no arguments, the no-argument constructor be invoked to initialize it.

Static or Class Fields
Per-object fields are usually what you need. You usually want a field in one object to be distinct from the field of the same name in every other object instantiated from that class.

Sometimes, though, you want fields that are shared among all objects of that class. These shared variables are known as class variablesvariables specific to the class as opposed to objects of the class.

Why would you want to use class variables? Consider, for example, the Sony Walkman factory. Each Walkman has a unique serial number. In object terms, each Walkman object has its own unique serial number field. However, the factory needs to keep a record of the next serial number to be assigned. You don't want to keep that number with every Walkman object. You'd keep only one copy of that number in the factory, or, in object terms, as a class variable.

You obtain class-specific fields by declaring them static, and they are therefore commonly called static fields. For example, a Point object to represent the origin might be common enough that you should provide it as a static field in the Point class:

public static Point origin = new Point();



If this declaration appears inside the declaration of the Point class, there will be exactly one piece of data called Point.origin that always refers to an object at (0.0, 0.0). This static field is there no matter how many Point objects are created, even if none are created. The values of x and y are zero because that is the default for numeric fields that are not explicitly initialized to a different value.

You can probably see now why named constants are declared static.

When you see the word "field" in this book, it generally means a per-object field, although the term non-static field is sometimes used for clarity.

The Garbage Collector
After creating an object with new, how do you get rid of the object when you no longer want it? The answer is simplestop referring to it. Unreferenced objects are automatically reclaimed by a garbage collector, which runs in the background and tracks object references. When an object is no longer referenced, the garbage collector can remove it from the storage allocation heap, although it may defer actually doing so until a propitious time.


We Are Founder..