Program to show Concept of CLASS in JAVA

Class: A collection of fields (instance and class variables) and methods. instance variable ( field variable or member variable)

Instance variables stores the state of the object. An instance variable is a variable that is defined in a class, but outside of a method. Each class would have its own copy of the variable. Every object has a state that is determined by the values stored in the object. An object is said to have changed its state when one or more data values stored in the object have been modified. When an object responds to a message, it will usually perform an action, change its state etc. An object that has the ability to store values is often said to have persistence. There is one copy of the variable for every instance (object) created from that class.

A common problem is trying to reference an instance variable from a static method. A static method (eg, main) can only reference static variables in its own class (or its own local variables).
class variable (aka static variable)

A class variable or static variable is defined in a class, but there is only one copy regardless of how many objects are created from that class. It's common to define static final variables (constants) that can be used by all methods, or by other classes.
We use class variables also know as Static fields when we want to share characteristics across all objects within a class. When you declare a field to be static, only a single instance of the associated variable is created common to all the objects of that class. Hence when one object changes the value of a class variable, it affects all objects of the class. We can access a class variable by using the name of the class, and not necessarily using a reference to an individual object within the class. Static variables can be accessed even though no objects of that class exist. It is declared using static keyword.
Color.blue is an example of a static final variable.

Class Methods – Static Methods: Class methods, similar to Class variables can be invoked without having an instance of the class. Class methods are often used to provide global functions for Java programs. For example, methods in the java.lang.Math package are class methods. You cannot call non-static methods from inside a static method.

Final Variable, Methods and Classes
In Java we can mark fields, methods and classes as final. Once marked as final, these items cannot be changed. Variables defined in an interface are implicitly final. You can't change value of a final variable (is a constant). A final class can't be extended i.e., final class may not be subclassed. This is done for security reasons with basic classes like String and Integer. It also allows the compiler to make some optimizations, and makes thread safety a little easier to achieve. A final method can't be overridden when its class is inherited. Any attempt to override or hide a final method will result in a compiler error.

Object: An object is an instance of a class created using a new operator. The new operator returns a reference to a new instance of a class. This reference can be assigned to a reference variable of the class. The process of creating objects from a class is called instantiation. An object encapsulates state and behavior.

An object reference provides a handle to an object that is created and stored in memory. In Java, objects can only be manipulated via references, which can be stored in variables.

Example: Class and Object initialization showing the Object Oriented concepts in Java

class Cube {

int length = 10;
int breadth = 10;
int height = 10;
public static int numOfCubes = 0; // static variable
public static int getNoOfCubes() { //static method
return numOfCubes;
}
public Cube() {
numOfCubes++; //
}
}

public class CubeStaticTest {

public static void main(String args[]) {
System.out.println("Number of Cube objects = " + Cube.numOfCubes);
System.out.println("Number of Cube objects = "
+ Cube.getNoOfCubes());
}
}

Output

Number of Cube objects = 0

Number of Cube objects = 0

Example 2: Creation of Cube objects by using the new operator.

public class Cube {

int length = 10;
int breadth = 10;
int height = 10;
public int getVolume() {
return (length * breadth * height);
}
public static void main(String[] args) {
Cube cubeObj; // Creates a Cube Reference
cubeObj = new Cube(); // Creates an Object of Cube
System.out.println("Volume of Cube is : " + cubeObj.getVolume());
}
}

No comments:

Google Search