- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Can we call a constructor directly from a method in java?
A constructor is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have same name as their class and, have no return type.
There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation.
The this keyword in Java is a reference to the object of the current class. Using it, you can refer a field, method or, constructor of a class.
Therefore, if you need to invoke a constructor explicitly you can do so, using "this()".
Invoking a constructor from a method
No, you cannot call a constructor from a method. The only place from which you can invoke constructors using “this()” or, “super()” is the first line of another constructor. If you try to invoke constructors explicitly elsewhere, a compile time error will be generated.
Example
import java.util.Scanner; public class Student { private String name; private int age; Student(){} Student(String name, int age){ this.name = name; this.age = age; } public void SetValues(String name, int age){ this(name, age); } public void display() { System.out.println("name: "+this.name); System.out.println("age: "+this.age); } public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the name of the student: "); String name = sc.nextLine(); System.out.println("Enter the age of the student: "); int age = sc.nextInt(); Student obj = new Student(); obj.SetValues(name, age); obj.display(); } }
Compile time error
Student.java:12: error: call to this must be first statement in constructor this(name, age); ^ 1 error
- Related Articles
- Can we call a method on "this" keyword from a constructor in java?
- Can we call run() method directly instead of start() in Java
- Can we call Superclass’s static method from subclass in Java?
- Can we call methods of the superclass from a static method in java?
- What will happen if we directly call the run() method in Java?
- How to call the constructor of a superclass from a constructor in java?
- Can we define a static constructor in Java?
- Can we have a constructor private in java?
- How can we call one constructor from another in the same class in C#?
- How can we call the invokeLater() method in Java?\n
- Can we declare a constructor as private in Java?
- How do we call a Java method recursively?
- How to call one constructor from another in Java?
- Java Program to Call One Constructor from another
- Can we return this keyword from a method in java?
