You are currently viewing Java – Object & Class

Java – Object & Class

Java is an Object Oriented Programming language. That means it works with the collection of objects interacting and transferring data between each other and more. And that bring’s up some questions like what is Java – Object & Class?

What is a Class?

  • Just think of a car as a class. A car has it’s own state/behavior. It is like a blueprint/template to create object.

Let’s see an example of a class:

First Java program ⇒ HelloWorld.java  ⇒

public class HelloWorld{
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

Output ⇒

C:\> javac HelloWorld.java
C:\> java HelloWorld 
Hello World

Car.java ⇒

public class Car {
  int speed = 50;
}

Here Car is the class and speed is an instance variable.

We can create multiple class in one class like:

Car.java ⇒

public class MyClass {
  int speed = 50;
}

class Car{
  public static void main(String[] args) {
    MyClass myObj = new MyClass();
    System.out.println(myObj.speed);
  }
}

What is an Object?

  • In the real world anything that you can make contact or have some specific state and behavior. In java, object is simile like that. Example: A Car could be an object because it has sate, color, behavior and more. An object is an instance of a class.

Let’s see an example of an object:

public class Car{
  int speed = 50;

  public static void main(String[] args) {
    Car myObj = new Car();
    System.out.println(myObj.speed);
  }
}

Here we can see we have a class car. And in the main method we have created an object of our car class like  Car myObj = new Car();. The instance variable is into the Car class. We can access that instance variable with the object that we have just created. To view that we have done it like this:  myObj.speed and with System.out.println()  we print it.

We can create as much object as we need but the variable name should be different and for this example we have created our object into the main method. But we can create object into methods as well and into others.

Let’s see an example of multiple methods:

public class Car{
  int speed = 5;

  public static void main(String[] args) {
    Car myObj1 = new Car();  // Object 1
    Car myObj2 = new Car();  // Object 2
    System.out.println(myObj1.speed);
    System.out.println(myObj2.speed);
  }
}

What is a Method?

  • Methods can be represented as behavior. Like the Car can turn right, turn left, go fast, go slow, can break. These are the methods a car behaves.

What is Instance Variable?

  • Each object has it’s unique set of instance variable. Values have been assigned to these instance variables and that creates the state of the object. 

I think we have now some concept of what is java Object & Class. If you have any issue with the environment then please go check the previous tutorial

We will discuss so many things on Object & Classes through the journey. We will be discuss about Constructors on our next post. So let’s dive in and stay put with AndroVaid.

Leave a Reply