logo codesdope

Classes and Objects in Java


In this section, you will learn to make your own class. Before going further, look at an example:

class Student {
  private String name;
  public Student(){
    name = "Unknown";
  }
  public void setName (String n){
    name = n;
  }
  public String getName(){
    return name;
  }
}

class Cl1{
  public static void main(String[] args){
    Student a = new Student();
    System.out.println(a.getName());
    a.setName("xyz");
    String b = a.getName();
    System.out.println(b);
  }
}
Output
Unknown
xyz

Let's go through this code to understand how to define our own class.
"Cl1" is our main class. Remember that as mentioned in the initial section, that the main class is a class which contains the main method ( public static void main(String [] args) ) and Java code begins its execution from this main class.

Without having the main class ( no class having the main method ), we can compile our code but can't execute it because the main method is executed first.

So, starting execution from 'Cl1' class ( as it is the main class ), Java will come to the line 'Student a = new Student();'. It means that 'a' is an object of the 'Student' class and for this 'Student' must be defined.

class Student - This is another class in our program. We have defined our own class named 'Student'. 'class' is a keyword which means that 'Student' is a class.
Inside the 'Student' class, we have a String variable 'name' and two methods 'getName' and 'setName'.
There is one more method whose name is same as its class name.

public Student(){
name = "unknown"
}

This method is called constructor. This method will be executed when objects of the class are created. This means that whenever we will create an object of the 'Student' class,

Student a = new Student();

this method will be called and the String variable 'name' will become equal to "unknown".

name = "unknown"

And the two methods 'setName' and 'getName' are defined to change and return the String variable 'name' respectively.

'Student a' is equivalent to saying that 'a' is a 'Student'. It will just mark 'a' as an object of the 'Student' class.
new Student() - new operator creates an instance of a class. So, now 'a' will have all the data members of the 'Student' class.

It means Student a = new Student(); will first tell that 'a' is a 'Student' and the new operator will then create Student's instance with all the data members.

object and class in java

We can also write Student a = new Student(); in two lines
Student a;
a = new Student();

private String name; - We have used 'private' here. It is called modifier. 'private' will restrict the direct use of the variable 'name' outside the 'Student' class. So, trying a.name = "xyz" will give you an error. Although, we can change and get variable the value of the variable 'name' by use of public methods 'setName' and 'getName' respectively. These methods are in the 'Student' class. So, they can access the private variable 'name' and since these methods are public, so we can call these methods outside the 'Student' class.
'public' is a modifier which allows the access of variables or methods from any class. More about modifiers will be discussed later.

Now coming to the main class, a.setName("xyz"); - We have called the method 'setName' here. To call any method, we use (.) DOT after the object and then call that method.
'a' is an object of the 'Student' class. So, 'a.setName' will call the method 'setName' of the 'Student' class for 'a'.

Let's sum up these things


class Student - defining a class whose name is 'Student'
public Student(){ } - constructor
private String - private restricts the use of variable 'name' outside the 'Student' class directly.
public void setName() and public String getName() - Two methods
public static void main(String []args){} - main method ( will be executed first )
Student a = new Student(); - make object 'a' of 'Student' class.
a.setName - calling method 'setName' of 'Student' class over object 'a'.

Constructor is a special type of method which is used to initialize an object. It is invoked at the time of object creation.
We can also make a constructor with nothing in its body.
public Student(){ };

Constructor having some parameters


class Rectangle{
  private int length;
  private int breadth;
  public Rectangle(int l, int b){
    length = l;
    breadth = b;
  }
  public int getArea(){
    return length*breadth;
  }
}

class Cl2{
  public static void main(String[] args){
    Rectangle r = new Rectangle(2,4);
    System.out.println(r.getArea());
  }
}
Output
8

In this example, we have parameters in our constructor. As told earlier, a constructor is also a method which is executed at the time of creating an object and has the same name as that of its parent class. So, it will function like a method and assign the passed values from Rectangle(2,4); to length and breadth respectively.

Rectangle r =new Rectangle(2,4);

It will create an object 'r' of the class 'Rectangle' and pass 2 to 'l' and 4 to 'b' ( 'l' and 'b' are used in the constructor of the class 'Rectangle' ).

object and class in java

object and class in java

Returning and passing object in a method


Yes, we can return or pass object(s) to a method. Let's see an example.

class Account{
  public int balance;
  public Account(){
    balance = 0;
  }
  public static Account getAcc(Account a, Account b){
    Account ac = new Account();
    ac.balance = a.balance+b.balance;
    return ac;
  }
}

class Cl3{
  public static void main (String[] args ){
    Account a1 = new Account();
    a1.balance = 50;
    Account a2 = new Account();
    a2.balance = 60;
    Account b = Account.getAcc(a1,a2);
    System.out.println(b.balance);
  }
}
Output
110

As you have seen, our method 'getAcc' is creating an 'Account' object by taking two 'Account' objects and is also returning it.
b = Account.getAcc(); - 'getAcc' will create and return a new 'Account' object and 'b' will become equal to that object.

Here, we have directly used 'getAcc' method on class 'Account' ( Account.getAcc() ) because 'getAcc' is a static method ( public static Account getAcc(Account a, Account b){ ). 'static' is explained below.
If there is no constructor in a class, the compiler automatically creates a default constructor.

Suppose we made a class Vehicle without any constructor, then automatically its constructor will be created with nothing in its body as follows.

class Vehicle{
Vehicle(){ }
}

Use of static


'static' is used to make access to any data variable or method without making an object of that class. It means that 'static' is used so that we can access any data variable or method without making an object of that class. Let's see this:

class Rectangle{
  public static void printArea(int l,int b){
    System.out.println(l*b);
  }
}

class Cl4{
  public static void main(String[] args){
    Rectangle.printArea(2,5);
  }
}
Output
10

Our method 'printArea' is static. So, we directly used it on the 'Rectangle' class, without making any object of it.

Don't spend so much time trying to choose the perfect opportunity that you miss the right opportunity.
-Michael Dell


Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.