Constructor | Method |
Constructor is a special method of a class but can’t be invoked directly by method call. | Methods are member of a class. |
It is not a member of a class as it can neither be inherited nor invoked using dot (.) operator. | Dot (.) operator is used to invoke Non static methods via object and static methods via class name. |
It has no explicit return type. | It has explicit return type, if there is nothing to return, the return type must be void. |
It has the same name as its class name. | Can have same name as its class name, but the existence of return type makes it a method. |
It is used to initialize the objects, members of object and then execute statements if any. | Used to execute statements. |
A no-argument, Default constructor is provided by compiler when no explicit constructor is there. In such case instance members are initialized with their default values (numeric data types are set to 0, char to ‘\0’ and reference variable to null). | Local variable must be initialized explicitly. |
If there is no this() as the first statement, super() will be there as first statement in the constructor. | ‘this’ is implicitly invoked on all the member in non-static methods, but need explicit invocation in case of name confliction. Syntax: this.x, this.go() |
The default constructor will call the default constructor of its super class due to presence of super() as its first statement. | ‘super’ can be used to explicitly invoke member of super class (specially used either in case of name conflict or to call method of super class when overridden in sub-class ) syntax: super.member: when name conflict super.method(args if any) : when overriden |
A constructor can never be abstract or static. | A method is of two types defined (implemented) or undefined (abstract). The method implementation can be further categorized as static or non-static. An abstract method can’t be static or final. |
Can be specified as public, none (default), protected or private. | Access-specifier public, none (default), protected or private are applicable. |
Can’t be final , native , or synchronized . (Constructor can take only access specifier) | Can be final, native, static or synchronized. |
Can be invoked by either a. new ClassName() b. this(args if any) c. super(args if any) d. getInstance() | Can be invoked by Class name in case of static method or by object/this in case of non-static method. |
Tuesday, December 10, 2013
Difference between Constructor and Method
Related Posts:
How to Create Custom Dialect class in Hibernate?Creating a Custom Dialect in Hibernate:In order to create any new Dialect class we need to extends org.hibernate.dialect.Dialect class. Dialect class is a abstract class.If you look at the Dialect class,public abstract c… Read More
Compiling and Running Java program by using another Java program.package com.ranga;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;/** Program: Compiling and Running Java program by using another Java program. java.lang… Read More
How to get all table names using Hibernate.This post demonstrates the how to get the all table names using Hibernate.Configuration configuration = new Configuration();configuration.configure("hibernate.cfg.xml");SessionFactory sessionFactory = configuration.buildSessi… Read More
Different ways to get the Connection object using HibernateGetting the java.sql.Connection object using Session:---------------------------------------------------------------------------org.hibernate.Session is nothing but get the one physical connection from the database. By u… Read More
Explain about System.out.println()System.out.println() System is a final class available in java.lang package. It is a system related class and most of times make native calls.Structure of System class:public final class System { public… Read More
0 comments:
Post a Comment