1.

What are the differences between constructor and method of a class in Java?

Answer»
ConstructorMethod
Constructor is used for initializing the object state.Method is used for exposing the object's behavior.
Constructor has no return type.Method should have a return type. Even if it does not return anything, return type is void.
Constructor gets invoked implicitly.Method has to be invoked on the object explicitly.
If the constructor is not defined, then a default constructor is provided by the java COMPILER.If a method is not defined, then the compiler does not provide it.
The constructor name should be equal to the CLASS name.The name of the method can have any name or have a class name too.
A constructor cannot be marked as final because whenever a class is inherited, the constructors are not inherited. Hence, marking it final doesn't make sense. Java throws compilation error saying - MODIFIER final not allowed hereA method can be defined as final but it cannot be overridden in its subclasses.
Final variable instantiations are possible INSIDE a constructor and the scope of this applies to the whole class and its objects.A final variable if initialised inside a method ensures that the variable cant be changed only within the scope of that method.


Discussion

No Comment Found