Java Language - Object Generating & Objects / Java classes

Java Object Generating & Objects

An object is an instance of a class. A class is a template or blueprint from which objects are created. So, an object is the instance(result) of a class. Object is a super class.

Object Definitions:

  • An object is a real-world entity.
  • An object is a runtime entity.
  • The object is an entity which has state and behavior.
  • The object is an instance of a class.

An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc. It can be physical or logical. The example of an intangible object is the banking system.



In Java, an object is created from a class. We have already created the class named Main, so now we can use this to create objects.

To create an object of Main, specify the class name, followed by the object name, and use the keyword new

public class A{

    x = 5;

    public static void main(String [] args){

        A  objOne  =  new  A();   //Object

        System.out.println(objOne.x);

    }

}

create two objects,

public class A{

    x = 5;

    public static void main(String [] args){

        A  objOne  =  new  A();  //Object 1

        A  objTwo  =  new  A();   //Object 2

        System.out.println(objOne.x);

        System.out.println(objTwo.x);

    }

}

  • State: represents the data (value) of an object.

    • Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
    • Identity: An object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely.


    Java Classes


    A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical.

    To create a class, use the keyword class

    Create a class named "A" with a variable a ,

    public class A {

        a  =  5;

    }


    A class in Java can contain:

    • Fields
    • Methods
    • Constructors
    • Blocks
    • Nested class and interface




























    Comments

    Popular posts from this blog

    Introduction of Java Programming Language

    Java Standards