what is constructor in Java? How it is important in Java?
A constructor in Java is a method that can have parameters, in which its name is the same as the name of the class. Example1 : class Ravi2 { public Ravi2() { // Your code here... } } It's used to instantiate the class (create an object), and to initialize the parameters if it's necessary. Instantiate Example1 : Ravi2 raviObject = new Ravi2(); ********************************************************** Consider now this Example2. Example2 : class Ravi2 { public Ravi2(String name) { System.out.println(name); } } Instantiate Example2 : Ravi2 raviObject = new Ravi2("Ravi"); ************************************************* Good luck.
thnx dear good response.....! now some confusion about instance Variables which are related to constructors....
plz ...tell me about executing process when we call the constructor from Class to the main method for execute?
If you are asking about how a program gets executed, then you should learn the Compilation, which we can summarize it as "The transformation of your code from a high level to the machine code, so your processor can understand it and execute it". To know how this happen, you should study compilation.
Join our real-time social learning platform and learn together with your friends!