aspose file tools*
The moose likes Beginning Java and the fly likes Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor" Watch "Constructor" New topic
Author

Constructor

Faisal Pathan
Greenhorn

Joined: Sep 01, 2004
Posts: 1
what is the actual purpose of constrcutor, if there is already a "new" keyword
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch, Faisal!

Let me turn the question around on you, a bit. What does new do?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
"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

Joined: Nov 06, 2000
Posts: 177
"Stamps out" sounds like something Smoky Bear does to fire. To clarify: "new" allocates memory for the object...
Bianca Will
Greenhorn

Joined: Sep 01, 2004
Posts: 1
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

Joined: Apr 04, 2004
Posts: 815
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.


I've heard it takes forever to grow a woman from the ground
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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 ]

Mike Gershman
SCJP 1.4, SCWCD in process
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
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

Joined: Mar 13, 2004
Posts: 1272
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

Joined: Apr 04, 2004
Posts: 815
Try what? Running a method on an unconstructed object? I certainly do doubt that that will pass.
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
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

Joined: Mar 13, 2004
Posts: 1272
(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

Joined: Jul 11, 2001
Posts: 14112
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


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
(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

Joined: Apr 20, 2006
Posts: 3344

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 ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor