You are currently viewing Java Object and Java Class with Example

Java Object and Java Class with Example

In this tutorial, you will learn about the concept of class and object in java with the help of examples. Using the object-oriented programming technique, we create a program by combining objects and classes.

In Java, an object is both a physical and logical entity, whereas a class is only a logical entity.

What is an object in Java?

Any entity with a state and behavior is referred to as an object. A Car, for example, is an object. it has 

A Car, for example, is an object. it has

  • States: idle, first gear, and so on.
  • Behaviors: Breaking, acceleration, and other actions are examples of behaviors.

There are three characteristics of an object:

  • The data (value) of an object is represented by its state.
  • The behavior (functionality) of an object, such as deposit, withdrawal, and so on, is represented by the term behavior.
  • A unique ID is typically used to represent an object’s identity. The value of the ID is hidden from the outside user. The JVM, on the other hand, uses it internally to uniquely identify each object.

For Example, a car is an object. Its name is BMW, color is black, known as its state. It is used to drive, so driving is its behavior.

  • A class’s instance is an object. A class is a blueprint or template from which objects are built. 

In Java, you can make an object.

This is how we can create a class object-

className object = new className();
// for Car class
Car sportsCar = new Car();
Car suvCar = new Car ();

What is a class in Java?

A class is a collection of objects with similar properties. It’s a blueprint or template from which objects are made. It’s a logical thing. It can’t be physical.

A class is an object’s blueprint. We must first define the class before we can create an object.

  • The class can be compared to a sketch of a house. It contains all of the information about the floors, doors, and windows, among other things. We construct the house based on these descriptions. The object is a house.
  • We can make many objects from a class because many houses can be built from the same description.

Create a class in Java

The class keyword in Java can be used to create a class. As an example.

class ClassName {
// fields
// methods
}

Methods are used to perform some operations and fields are used to store data.

We can create a class for our car object as follows:

class Car {
// state or field
private int gear = 3;
// behavior or method
public void breaking() {
System.out.println("Breaking");
}
}
  • We created a class called Car in the preceding example. It has a field called gear as well as a method called breaking ().
  • Car is a prototype in this case. Using the prototype, we can now make any number of Cars. Furthermore, the prototype’s fields and methods will be shared by all of the cars.

Java Object and Java Class,Java Object and Java Class,Java Object and Java Class,Java Object and Java Class,Java Object and Java Class,Java Object and Java Class,Java Object and Java Class,class and object in java,class and object in java,class and object in java,class and object in java,class and

Example: Java Class and Objects

class Switch {
  

  boolean On;

// a method to turn the light on
  void turnOn() {
    On = true;
    System.out.println("Light is On? " + On);

  }

  //a method to turnoff the light
  void turnOff() {
    On = false;
    System.out.println("Light is On? " + On);
  }
}

class Main {
  public static void main(String[] args) {

    // create objects led and halogen
    Switch AnchorSwitch = new Switch();
    Switch SkySwitch = new Switch();

    
    AnchorSwitch.turnOn();
    SkySwitch.turnOff();
  }
}
Light is On? true
Light is On? false

  • We created a Switch class in the preceding program. It has two methods: turnOn() and turnOff(), as well as a variable called On ().
  • Two Switch class objects, AnchorSwitch and SkySwitch, were created inside the Main class. The objects were then used to call the class’s methods.
  • The On variable is set to true and the output is printed by AnchorSwitch.turnOn().
    SkySwitch.turnOff() prints the output and sets the On variable to false.
    An instance variable is a variable that is defined within a class. It’s because when we make a class object, we call it an instance of the class. And the variable will be duplicated in each instance.

Three Ways to initialize object

In Java, there are three ways to initialize an object.

  • By reference variable
  • By method
  • By constructor

Example of an Object and a Class: Initialization by Reference

The term “initialize” refers to the process of storing data into an object. Let’s look at a simple example where we’ll use a reference variable to initialize the object.

class Sample{  
 int id;  
 String name;  
}  
class SamplePost{  
 public static void main(String args[]){  
  Sample x=new Sample();  
  x.id=1;  
  x.name="Sagar";  
  System.out.println(x.id+" "+x.name);
 }  
}  

Output:

1 Sagar

We can also create multiple objects and use reference variables to store information in them.

Example of an Object and a Class: Initialization via a method

In this example, we create two objects of the Sample class and use the setdata() method to set the value of these objects. By calling the getdata() method, we can see the state (data) of the objects.

class Sample{  
 int rollnumber;  
 String name;  
 void setdata(int r, String n){  
  rollnumber=r;  
  name=n;  
 }  
 void getdata(){System.out.println(rollnumber+" "+name);}  
}  
class SamplePost{  
 public static void main(String args[]){  
  Sample x=new Sample();  
  Sample y=new Sample();  
  x.setdata(1,"Sagar");  
  y.setdata(2,"Avdhesh");  
  x.getdata();  
  y.getdata();  
 }  
}  

Output:

1 Sagar
2 Avdhesh

You may like:

Java Array with Example

We hope that this article will assist you in understanding all about Object and Java Class with Example. We have concentrated on making a basic, meaningful, and easy-to-learn guide to the concepts. Still, if you have any problems regarding this, please post them in the comment section, we will be glad to assist you.

This Post Has 3 Comments

Leave a Reply