Constructor and Method?

Constructor and Method?

What is the Difference Between Constructor and Method? It is very important to understand these basic concepts in order to have build a better development career. Moreover these are the most commonly asked interview questions in any technical interview rounds. Let’s start understanding this one by one:- 

Constructor

  • Java constructor name must be same as the class name.
  • Constructor chiefly used to initialize the state of an object.
  • Constructor should not have any return type.
  • Constructor will be called automatically whenever object is created.
  • Constructor cannot be a final, abstract, static and synchronized.
  • The java compiler provides a default constructor if we do not have any constructor in a java program.

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.

class Person

{

int id;//data member

String name;//data member

Person()

{

System.out.println(” this is a default constructor “);

}

Person(int id, String name)

{

System.out.println(” this is a parameterized constructor “);

this.id = id;

this.name = name;

System.out.println(id+ ” “+name);

}

public static void main(String args[])

{

Person s = new Person();//default constructor will be call

Person s1 = new Person(101, ” ABC”);//parameterized constructor will be called

}

}

Output : this is a default constructor

             this is a parameterized constructor

             101 ABC

Constructor vs Method

Method

  • Method name may or may not be same as class name.
  • Method is used to expose the behavior of an object i.e action or task or operation.
  • The java compiler does not provide any default or parameterized method in java.
  • Method must have a return type.
  • Method should be called explicitly either with a class reference or object reference.
  • 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.

class Pristine

{

static void courses()//static method

{

System.out.println(“Valuable Courses in Ludhiana By Pristine”);

}

void grooming()

{

System.out.println(“With Courses provide other activities for grooming”);

}

public static void main(String args[])

{

Pristine.courses();//static method can be access by using class name

Pristine e = new Pristine();

e.grooming();//need object reference to call method

}

}

Output:

Valuable Courses in Ludhiana By Pristine            

With Courses provide other activities for grooming

 

Constructor Method
A constructor is used to initialize the state of an object. A method is used to expose the behavior of an object that means to perform some task.
Constructor is invoked implicitly at the time of object creation. Method is invoked explicitly by the user when needed.
Constructor name must be same as the class name. Method name may or may not be same as class name.
Constructor must not have a return type. Method must have a return type.
The java compiler provides a default constructor if you don’t have any constructor. Method is not provided by compiler in any case.

You can also refer YouTube video for more help to understand this article by clicking on the link as given below:

Happy Coding!

9 thoughts on “Constructor and Method?

  1. I see you don’t monetize your page, don’t waste your traffic,
    you can earn additional bucks every month with very good adsense alternative for blogs like yours.

    For more info search in google: blackhatworren’s strategies

  2. I often visit your page and have noticed that you don’t update
    it often. More frequent updates will give your website
    higher rank & authority in google. I know that writing articles
    takes a lot of time, but you can always help yourself with miftolo’s tools which will shorten the time of creating an article
    to a couple of seconds.

  3. Hello. I see that you don’t update your blog
    too often. I know that writing content is boring and time consuming.
    But did you know that there is a tool that allows you to create new posts using existing content
    (from article directories or other blogs from your niche)?
    And it does it very well. The new articles are high quality and pass the copyscape test.
    You should try miftolo’s tools

  4. Hi. I see that you don’t update your site too often.
    I know that writing articles is time consuming
    and boring. But did you know that there is a tool
    that allows you to create new posts using existing content (from article directories or other websites from your niche)?
    And it does it very well. The new articles are unique and pass the
    copyscape test. You should try miftolo’s tools

  5. I have noticed you don’t monetize your website, don’t waste your traffic, you can earn additional cash every month.
    You can use the best adsense alternative for any type of
    website (they approve all websites), for more info simply search
    in gooogle: boorfe’s tips monetize your website

  6. I think this website has some really wonderful information for everyone :D. “The test of every religious, political, or educational system is the man that it forms.” by Henri Frdric Amiel.

  7. I have to convey my admiration for your kind-heartedness giving support to men and women who really want assistance with this field. Your special commitment to getting the message along had become certainly informative and have always enabled those just like me to arrive at their objectives. Your personal warm and helpful advice means so much a person like me and far more to my office colleagues. Many thanks; from each one of us.

Leave a Reply

Your email address will not be published. Required fields are marked *