Java Object and Classes

Object − Objects have states and behaviors. Example: A cat has states – color, name, breed as well as behaviors – wagging the tail, mau-mau, eating. An object is an instance of a class. Class – A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.

Objects in Java

if we consider the real world, we can find many objects around us, cars, dogs, humans, etc. All these objects have a state and behavior. If we consider a cat, then its state is – name, breed, color, and the behavior is – mau-mau, wagging the tail, running. A software object’s state is stored in fields and behavior is shown via methods

Classes in Java

A class is a blueprint from which individual objects are created.

For example:

public class cat {
   String breed;
   int age;
   String color;

   void mau() {
   }

   void jumping() {
   }

   void sleeping() {
   }
}

A class can contain any of the following variable types.

  • Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
  • Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
  • Class variables − Class variables are variables declared within a class, outside any method, with the static keyword.

A class can have any number of methods to access the value of various kinds of methods. In the above example, mau(), jumping() and sleeping() are methods.

Constructors

A constructors have same name as the class. A class can have more than one constructor. Following is an example of a constructor:

public class Puss{
   public Puss() {
   }

   public Puss(String name) {
      // This constructor has one parameter, name.
   }
}

Creating an Object

Object can be created from a class .In java by using new keyword for creating new objects.

Example:

public class Puss {
   public Puss(String name) {
      // This constructor has one parameter, name.
      System.out.println("cat Name is :" + name );
   }

   public static void main(String []args) {
      // Following statement would create an object myPuss
      Puss myPuss = new Puss( "Puss" );
   }
}

Accessing Instance Variables and Methods

Consider the example shown below:

/* First create an object */ObjectReference = new Constructor();

/* Now call a variable as follows */ObjectReference.variableName;

/* Now you can call a class method as follows */ObjectReference.MethodName();

public class Puss {
   int puppyAge;

   public Puss(String name) {
      // This constructor has one parameter, name.
      System.out.println("Name chosen is :" + name );
   }

   public void setAge( int age ) {
      puppyAge = age;
   }

   public int getAge( ) {
      System.out.println("Puss age is :" + puppyAge );
      return puppyAge;
   }

   public static void main(String []args) {
      /* Object creation */      Puss myPuppy = new Puss( "Puss" );

      /* Call class method to set puppy's age */      myPuppy.setAge( 2 );

      /* Call another class method to get puppy's age */      myPuppy.getAge( );

      /* You can access instance variable as follows as well */      System.out.println("Variable Value :" + myPuppy.puppyAge );
   }
}

output

Name chosen is :puss
Puppy's age is :2
Variable Value :2

Import Statements

which includes the package and the class name is given, then the compiler can easily locate the source code or classes. For example:

import java.io.*;
Related Post