- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between Constructors and Methods in Java
Constructors are special methods used to initialize objects whereas methods are used to execute certain statements. Following are the important differences between Constructors and Methods.
Sr. No. | Key | Constructors | Methods |
---|---|---|---|
1 | Purpose | Constructor is used to create and initialize an Object . | Method is used to execute certain statements. |
2 | Invocation | A constructor is invoked implicitly by the System. | A method is to be invoked during program code. |
3 | Invocation | A constructor is invoked when new keyword is used to create an object. | A method is invoked when it is called. |
4 | Return type | A constructor can not have any return type. | A method can have a return type. |
5 | Object | A constructor initializes an object which is not existent. | A method can be invoked only on existing object. |
6 | Name | A constructor must have same name as that of the class. | A method name can not be same as class name. |
7 | Inheritance | A constructor cannot be inherited by a subclass. | A method is inherited by a subclass. |
Example of Constructor vs Method
JavaTester.java
public class JavaTester { int num; JavaTester(){ num = 3; System.out.println("Constructor invoked. num: " + num); } public void init(){ num = 5; System.out.println("Method invoked. num: " + num); } public static void main(String args[]) { JavaTester tester = new JavaTester(); tester.init(); } }
Output
Constructor invoked. num: 3 Method invoked. num: 5
- Related Articles
- Difference between the list() and listFiles() methods in Java
- What is the difference between non-static methods and abstract methods in Java?
- What is the difference between getter/setter methods and constructor in Java?
- Difference between find() and findOne() methods in MongoDB?
- Difference between shift() and pop() methods in Javascript
- Difference between push() and unshift() methods in javascript
- Difference between test () and exec () methods in Javascript
- Difference between BeforeClass and BeforeTest methods in TestNG
- Difference between title() and wm_title() methods in Tkinter class
- Explain the difference between functions and Methods in Swift
- Differences between wait() and join() methods in Java
- Constructors in Java\n
- What is the difference between functions and methods in JavaScript?
- Low level difference between Slice and Splice methods in Javascript
- Differences between takewhile() and dropWhile() methods in Java 9?

Advertisements