This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes regarding constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "regarding constructor" Watch "regarding constructor" New topic
Author

regarding constructor

Gursewak Singh
Ranch Hand

Joined: Jan 09, 2011
Posts: 79

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

Joined: Aug 31, 2007
Posts: 791

Whenever new object of a class created then constructor of class is executed.


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Gursewak Singh
Ranch Hand

Joined: Jan 09, 2011
Posts: 79

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

Joined: Jan 09, 2011
Posts: 79

Mean to say i want to know more about the use of constructor
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

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


The important thing is not to stop questioning.Curiosity has its own reason for existing.
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 791

@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

Joined: Jul 09, 2011
Posts: 1
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
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Gursewak Singh
Ranch Hand

Joined: Jan 09, 2011
Posts: 79

Ok,
Thank you Henry,Himanshu,Ninad And Vijay.

now i have better idea about constructors

thank you very much.....
 
GeeCON Prague 2014
 
subject: regarding constructor