• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding constructor

 
Gursewak Singh
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

i have a question which is of no sense,but it is confusing me,
And Question is

As we can initialize a variable in class then why we use constructor.
As we know main purpose of constructor is to initialize a instance variables
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever new object of a class created then constructor of class is executed.

 
Gursewak Singh
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that Whenever new object of a class created then constructor of class is executed.
but what is need of it if we can initialize a instance variable without it?

as we know basically constructor are use to initialize a instance variable
 
Gursewak Singh
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mean to say i want to know more about the use of constructor
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gursewak,

As you already know that the constructor is used to initialize the variables.

Lets take the following example.


So by means of constructor the things got simpler,to initialize the variable to appropriate state.


Hope that will help you.
Thanks
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Gursewak
Go through Constructor Declarations and Initialization of Fields you will understand use of constructor.

You can initialize instance variable either in constructor or in instance initialization block, it may depends on requirement.

See the code given below

Output of above code is given below
First Instance Block
Second Instance Block
Constructor


While creating an instance after instance initialization block constructor will execute.

I hope this may help you.

 
himanshu jaggi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors are used to initialize the objects only once as soon as a new object is created by using new keyword.
constructors must have its name same as class name and also only placed after new keyword.
constructors are called only once so that no other method can call it.


Better example:- doctors(constructors) use to give all the necessary vaccinations to a newly born child(object) only once.
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gursewak Singh wrote:
but what is need of it if we can initialize a instance variable without it?

as we know basically constructor are use to initialize a instance variable


I am assuming that you are just learning Java -- or even just learning object oriented programming. At this point in your training, objects are probably nothing more than just data structures; and class definitions are probably just a bit of business logic with some instance variables.

As your training continues, constructing an object may include very complex initialization processes. These processes may construct tons of other objects, access databases, access messaging systems, make tons of calls to services, and have tons of combinations to construct your class instances. In that regard, it will probably be much more clearer to why you should provide constructors, than expect your users to configure everything correctly for your instance.

Henry
 
Gursewak Singh
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,
Thank you Henry,Himanshu,Ninad And Vijay.

now i have better idea about constructors

thank you very much.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic