Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Re: Constructors

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't seem to grasp the complete concept of constructors. I know it is for initializing objects, but I don't know why in a method you need to make a call to the constructor. Another thing, I don't really understand the call to the superclass' constructor.
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, constructors are used to initialize objects. If you inherit a superclass, you want to make sure it's variables are initialized correctly. That's why java/compiler insists that a call be made to the superclass constructor. If you don't do it yourself, the compiler will make a call to the no-argument constructor in the superclass...hopefully there is one.
Bosun
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't make a call to a constructor during a method, only during other constructors. The constructor is called when the "new" operator is used to create an instance of the class. The constructor sets up all the attributes of the class (you know - the "has a" things).
If you have class Dog, and every animal "has a" breed, then when you make a particular instance of Dog the constructor had better create a variable called "breed". However since Dog is a subclass of Animal, and every Animal "has a" species, somehow you have to get that attribute of the superclass created also. If you don't need to send in any parameters to get things set up correctly then you don't need to explicitly call the super constructor, the compiler will call the default one for you. However in this case the Dog instance needs to pass the species "dog" as a paramter to the Animal class. Therefore in the Dog constructor the first line is a call to super(dog).
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are calling an object's constructor when you use the new keyword to create the object. Some objects (ok, most objects) have members that contain special information about that object. For example, a book object might have a member variable called title and one called author. Referring to a book is hard without knowing the title and the author, so this info is needed when we create a new book object. How are we going to pass that info to our book that we are creating? Using the constructor. We write a constructor for our book object that takes two string arguments.
Let's further suppose that we want to subclass our book object to create an object called a programming book. ProgrammingBook objects will also have a member called language. When we need to create a new ProgrammingBook object we do it like this:
<PRE>
public class ProgrammingBook extends Book{
public ProgrammingBook(String author, String title, String language){
super(author, title);
this.language = language;
}
...
book bookOfTheMonth = new ProgrammingBook("Exploring Java", "Niemeyer & Peck", "Java");
</PRE>
In this case, we called super() to make use of our object's parent's constructor. Since Book already knows what to do with an author and a title, we just pass those on up to the Book constructor by calling super(). Then we make use of ProgrammingBook's member, language.
Does this help?
Bodie
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing about constructors, first you have to create the object then you need a constructor to use the object that you created, right?

for example:
// object
Point origin_one = new Point(23, 94);
public class Point {
public int x = 0;
public int y = 0;
//A constructor!
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
But why do you use this dot with the this keyword?
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have sort of wandered into a different topic. In the constructor in your example the instance has 2 variables x and y which you pass into the constructor. However the x and y in the constructor are NOT the same x and y, they are local to the constructor and will die when the constructing is over.
So the this.x =x is used to say "set the instance variable of this object = local variable x".

And to be picky - you can't really use a constructor EXCEPT to create an object.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Becky,
You have to use the "this" keyword because the constructor parameters happen to have the same name as the two instance variables (x and y). If you had named the constructor params something else (like xx and yy), you would not need to use "this".
Make sense?
~Ryan
 
Becky Miller
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so the "this" keyword is used to access the instance variables:
public int x = 0;
public int y = 0;
and not the constructors parameters.
 
Becky Miller
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this code Pass is creating an object new Pass, so how come there's no constructor to initialize the object? Thanks.
public class Pass{
static int j=20;
public static void main(String args[]){
int i =10;
Pass p = new Pass();
p.amethod(i);
System.out.println(i);
System.out.println(j);
}
public void amethod(intx){
x = x*2;
j = j*2;
}
}

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't supply a constructor, the one with no parameters (the default constructor) gets used. In the example you gave,
<PRE>
Pass p = new Pass();
</PRE>
you are using this default constructor. Notice that after the new keyword there is a method call to a method called Pass()? That is the constructor. You didn't write it, but it is there anyway.
Also, if you do write a constructor but you don't call super() in it, the compiler will do that for you anyway.
 
Becky Miller
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that
Pass p = new Pass();
is creating the object, so how could this be a constructor?
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you write, "Pass p = new Pass();", you are calling the Constructor, and the constructor helps in creating a new object.
If you remember, the constructor for Pass is "Pass() { }", which should look a little familiar...
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can often create an object in many ways. If the author of the class was considerate enough to supply lots of overloading constructors then you get your choice.
For instance there is a class called Color. I can create a color by saying
Color c = new Color(144, 200, 20); and the constructor which takes 3 integers is called to create a color with that combination of red, green and blue.
Or you can say
Color c = new Color(magenta); and since the author supplied a whole bunch of built in color variables, the constructor that uses the built in variables is called.
There is also another constructor that allows you to feed in the red, green and blue integer values and a fourth value for intensity. Now the author probably would not repeat the code in two constructors just because one does more than the other. I would expect that the first line of this constructor is a call to this.Color(r, g, b) to use the code there (it got those parameters in also) and then the intensity funcionality is added to that.
etc.
[This message has been edited by Cindy Glass (edited January 26, 2001).]
[This message has been edited by Cindy Glass (edited January 26, 2001).]
 
He does not suffer fools gladly. But this tiny ad does:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic