• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor

 
Faisal Pathan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the actual purpose of constrcutor, if there is already a "new" keyword
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constutor is there to provide safe construction of a new instance of an object. The new keyword is just (I suppose) an explicit call to the constructor.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Faisal!

Let me turn the question around on you, a bit. What does new do?
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"new" stamps out the memory. The constructor is the routine where you can initialize it. Furthermore, by adding parameters you can dictate what information has to be provided by the calling routine when the object is created.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Stamps out" sounds like something Smoky Bear does to fire. To clarify: "new" allocates memory for the object...
 
Bianca Will
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructor is just like any other method we define in a class only thing is that it has same name as that of class.new actually allocates memory to the object.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, now, let's not get to hasty here, Bianca... it's a little more then THAT.

Constructers sort of set everything up. Prior to the constructor call, the object is still null. Any initialization you have outside of a constructer isn't run until a constructer is called... it does a little more that just call a method.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my view, constructors are misnamed - they alter already initialized variables in already created objects, based on the arguments passed to the constructors in the new syntax and on other sources.

Memory allocation for an object is done by the JVM based on information in the class object. The JVM also initializes the instance variables to their default values. Next, the variable initializers and the initialization blocks are executed together in the order they appear in the source code. They can alter the values of the variables. Finally, the constructors get a chance to modify the variables once again. At that point, the object is considered fully initialized and final variables can no longer be altered.

I have not considered the implications of super() here.

BTW, constructors are not methods. Unlike methods, constructors can alter final variables. There are constraints on constructors that don't apply to methods. And you can't call a constructor directly.
[ September 01, 2004: Message edited by: Mike Gershman ]
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Gershman:
In my view, constructors are misnamed - they alter already initialized variables in already created objects,



erhm... if I'm not mistaken, that's not correct... They variables are not already created, nore are the objects. try and run a method on either before a constructer, and brace your self for a fat NullPointerException
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try and run a method on either before a constructer, and brace your self for a fat NullPointerException


An instance initialization block can freely manipulate a class's variables and call its methods while only the first line of the constructor body [super() or this()] has executed.

So say my books. If you doubt it, try it.

That's why I don't like the term "constructor". Constructors don't actually construct objects, they update them.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try what? Running a method on an unconstructed object? I certainly do doubt that that will pass.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We're getting a little obscure here, but reading "Java Language Specification" section "12.5 Creation of New Class Instances" I see that the sequence is (1) "memory space is allocated for the object with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type," (2) the constructor is run, which means (2a) run the super() constructor (if super is used) (2b) execute instance variable initializers, and (2c) execute the body of the constructor.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(1) "memory space is allocated for the object with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type," (2) the constructor is run, which means (2a) run the super() constructor (if super is used) (2b) execute instance variable initializers, and (2c) execute the body of the constructor.


(2a) simply climbs the parent/child tree until the root (Object) is reached to set up the construction sequence and possibly pass up constructor arguments. 2b and 2c are then executed at each level starting with Object and going down (2b-0 2c-0 2b-1 2c-1 2b-2 2c-2 etc.) The instance initializers, both as part of variable declarations and in instance instantiation blocks, can do anything a method can do, including calling methods. Only after the intializers have finished at a level are the constructor bodies executed.

Running a method on an unconstructed object? I certainly do doubt that that will pass.

"Unconstructed" is a misnomer, but I did put some interesting stuff (printing an instance variable) in an instance initialization block and it worked fine. Variables have memory and initial values before the "constructor" body gets past its first line.

BTW, this CAN be on the SCJP exam. From Kathy Sierra's SCJP book:
You cannot make a call to an instance method, or access an instance variable, until after the super constructor runs.

Note the word super

Give it a try.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joseph George:
Try what? Running a method on an unconstructed object? I certainly do doubt that that will pass.




Output is

42
constructor
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(By the way, that example includes an "instance initializer block" -- it's in the language because without it there would be no way to initialize anonymous inner class instances.)
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Gershman:
..BTW, constructors are not methods. Unlike methods, constructors can alter final variables.


I know its a pretty old thread and just stepped in here when i was searching for a different thread. As i felt there is a contradiction rather clarification required here in the above quoted lines, had to reply to this thread.

The constructor allows you to postpone the initialization of the final variable until the end of constructor body.

Having said that Constructor is not an ordinary method, how does it allow you to modify a final variable ever? I don't think it was right.

No matter what, the final variables are allowed to have a value once and only once. Whether you do it in in the instance initialization block or in the constructor, once it is done the compiler is sure enough to warn you for having initialized the final variable already in the subsequent places.

In such case, the constructor does NOT allow you to modify a final variable. Right?
[ January 24, 2008: Message edited by: Raghavan Muthu ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic