• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regarding constructor

 
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mean to say i want to know more about the use of constructor
 
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok,
Thank you Henry,Himanshu,Ninad And Vijay.

now i have better idea about constructors

thank you very much.....
 
reply
    Bookmark Topic Watch Topic
  • New Topic