• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the difference between these two constructors?  RSS feed

 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this code:



Now, consider this code:



I know that both of these are constructors. In the second example, we are instantiating a Vehicle object and assigning it to the reference "car." Then, what kind of constructor are we dealing with in the first example? Why are we declaring a variable inside a constructor? Shouldn't an instance variable be declared outside of a constructor?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Richardson wrote:I know that both of these are constructors.


I guess you could say that... but when you do, you're being rather vague. The first example declares a constructor for the Vehicle class, so you could say it "is" a constructor. Whereas the second example uses that constructor to create a Vehicle object, saying it "is" a constructor is a stretch. So that's the difference between the two.

As for your second question:

Why are we declaring a variable inside a constructor?


I dunno. You're the one who wrote the code... so why did you choose to declare a variable there? Not that there's anything wrong with that, it's perfectly legitimate to declare a local variable in a constructor, just like in any other block.

Shouldn't an instance variable be declared outside of a constructor?


Yes... but what your example shows isn't declaring an instance variable. It's declaring a local variable.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark,
I think your second example may be an attempt at a Factory?  You have a problem though of assigning the instanciated object to a varialbe inside the class body, so you never return it to the caller. As a result you have a Class with no constructor or take a default constuctor which would fall back to the Object class--so basically nothing fancy at all. Calling that a constructor, IMO, would lead to saying every case of instanciation is in itself an orphaned construtor--which it is not.
 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Mark Richardson wrote:I know that both of these are constructors.


I guess you could say that... but when you do, you're being rather vague. The first example declares a constructor for the Vehicle class, so you could say it "is" a constructor. Whereas the second example uses that constructor to create a Vehicle object, saying it "is" a constructor is a stretch. So that's the difference between the two.

As for your second question:

Why are we declaring a variable inside a constructor?


I dunno. You're the one who wrote the code... so why did you choose to declare a variable there? Not that there's anything wrong with that, it's perfectly legitimate to declare a local variable in a constructor, just like in any other block.

Shouldn't an instance variable be declared outside of a constructor?


Yes... but what your example shows isn't declaring an instance variable. It's declaring a local variable.



Thank you, Paul.

I have been using constructors in the following form for a while now:



I never had to declare a constructor as shown in the first example. I guess my next question is: What is the point of declaring a constructor? My assumption was that the class declaration and it's instance variables are the de facto "declaration" for a constructor. In other words, when I instantiate an object using the new keyword, the constructor creates an object on the basis of the class and it's instance members.

So, now what is this constructor that we are declaring? And, why would we want to have a local variable inside a constructor?
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,

When you instantiate an object by using new, you call the constructor associated with that class.  If no constructor is declared, then the class is initialized using the constructor of the parent class even if it is back to the Object class,

A constructor as shown

Give you the opportunity to do something you consider significant on intantiation of the object.
Let's just say for argument sake we have some prssing need to assign a random ID, non unique, to each object as it is instantiated. We could do the following:

We can also add parameters to the constructor,.

This way we could assign our own objectID to each object we instatiate.  We can also have multiple constructors with distict signatures.

So if we instantiate a Junk object withut an int arguement we get a default reandom int assigned, Junk j = new Junk();
but if we want to assign an objectID to the object we can do it this way: Junk j = new Junk(5);

Given these are very simple, but the concept extends to anything you can imagine.

Please note--this is not checked code, and it is very late and I am sur there are typo's, but the concept is correct.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Richardson wrote:I never had to declare a constructor as shown in the first example. I guess my next question is: What is the point of declaring a constructor? My assumption was that the class declaration and it's instance variables are the de facto "declaration" for a constructor. In other words, when I instantiate an object using the new keyword, the constructor creates an object on the basis of the class and it's instance members.

So, now what is this constructor that we are declaring? And, why would we want to have a local variable inside a constructor?


It's quite often the case that declaring instance variables is sufficient to initialize the state of an object of the class, and you don't need to declare any constructors. But it's also quite often the case that you need a constructor to complete the initialization of an object. For example:



Here, when you use that constructor you're going to provide it with information which wasn't known at compile time, namely the VIN of the Car you're creating. Presumably this data would have come from some external source like a database or a person at a keyboard.

As for why you'd want to have a local variable inside a constructor: in the example you posted, you wouldn't. No more than you would want to have a local variable which is never used in any other piece of Java code. But a constructor can contain any Java code, so why not local variables as well? How about this:



Here you've got two local variables involved in initializing the object's state.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!