Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday 26 July 2017

Difference Between Constructor and Method In Java

Java Constructor vs Method

Difference Between Constructor and Method In Java

Now here we will discuss some differences between Constructor and Method in java programming language. This is the very important question for interview point of view. So let's starts....

First we discuss about java constructor.


Constructor

  • Java constructor basically used to initialize the state of an object i.e putting a value into the data member of a class.
  • Java constructor name must be same as the class name.
  • Java constructor should not have any return type(even void).
  • Constructor will be called automatically whenever object is created.
  • The java compiler provides a default constructor if we do not have any constructor in a java program.
  • Java constructor cannot be a final, abstract, static and synchronized.


Java Constructor Example

In this example, we are going to use default constructor and parameterized constructor. Both constructor will be called automatically when object is created.

public class Student
{
int rollno;//data member
String name;//data member
Student()
{
System.out.println(" this is a default constructor ");
}
Student(int rollno, String name)
{
System.out.println(" this is a parameterized constructor ");

this.rollno = rollno;
this.name = name;
System.out.println(rollno+ " "+name);
}
public static void main(String args[])
{
Student s = new Student();//default constructor will be call
Student s1 = new Student(101, " raghu");//parameterized constructor will be called
}
}

output : this is a default constructor
              this is a parameterized constructor
              101 raghu


Method

  • Method is used to expose behavior of an object i.e action or task or operation.
  • Method name may or may not be same as class name.
  • Method must have return type.
  • Method should be called explicitly either with class reference or object reference.
  • The java compiler does not provide any default or parameterized method in java.
  • Method can be final, abstract, static and synchronized.



Java Method Example

In this example, we will define a class in which we take some methods to perform some specific task or action or operation.

public class Employee
{
static void work()//static method
{
System.out.println("Employee work as a senior java developer");
}
void manage()
{
System.out.println("Employee manage all junior java developer");
}
public static void main(String args[])
{
Employee.work();//static method can be access by using class name
Employee e = new Employee();
e.manage();//need object reference to call method
}
}

output : Employee work as a senior java developer
              Employee manage all junior java developer

Q. When use default constructor and when use paremeterized constructor in java.

Ans. When we want to provide the default value or same value to each object then we use default constructor in java and when we want to provide different-different values to each object then we use parameterized constructor in java.

Q. Can we make any constructor as private constructor ?.

Ans. Yes, we can make any constructor as private constructor in java but if we make any constructor as private then we cannot create an object of that class to outside of a class.

Q. Can we override main method in java ?.

Ans. No, We cannot override main method in java.

Share:

3 comments:

  1. Your blog is very informative for java learner.

    ReplyDelete
  2. In this tutorial, you are explained very well. For more detail, learn Java constructor with example here.

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate