• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Constructors...

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am a bit confused on this topic and it may be a bit too general but ..
When do you need to define the no parameter constructor explicitly ?
When dont you need to ?
Thanks,
nt
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have something that you need to accomplish when the instance is created - like setting up some variables or whatever, and you don't need any input from the calling code in the form of parameters, then you use a no-argument constructor.
If you don't have any tasks to do then you can just not have any constructors and the JVM will supply a no-argument constructor for you. If you have EVEN ONE other constructor then it becomes your job to supply the no-argument constructor also - if it is a valid thing to do.
It is possible that there is NO CASE that you could create an instance of the class without having some parameters supplied. In that case it might be correct to NOT have a no-argument constructor. Anyone trying to create an instance of it with no parameters will get a compile error.
 
nt novel
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me be more specific.
In a class when I have a constructor with a parameter.
I have seen examples where they dont explicitly define the default constructor i.e, with no params.
In some cases I see a question.. where the answer would be no default constructor is define and hence a compiler error is generated...
Hope i am clear this time.
Thanks for the answer.
I mean in such case when

Originally posted by nt novel:
Hi all,
I am a bit confused on this topic and it may be a bit too general but ..
When do you need to define the no parameter constructor explicitly ?
When dont you need to ?
Thanks,
nt


 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think I got ur question.
When u are not creating any constructor on ur own then a default constructor is provided which takes no arguments. So in this case u can create objects of class using this default constructor which u have not created.
But once u define any constructor other than default constructor say which takes one args or two args then the default constructor is not provided. So u can create objects using ur constructors but u can't create objects using defaullt constructor coz u have not defined it and u r not provided with a default constructor.
Neeraj Thakkar
------------------
Neeraj Thakkar
Java Consultant
 
nt novel
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neeraj,
Thanks for the effort.
But I have seen examples like
class A {
public A (int i ) { }
}
class B extends A {
A a = new A(2);
}

Now is this correct or incorrect ?
If the answer is Yes >... That is the root of my consfusion.
Where is the default constructor there ???
Thanks again.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello nt,
Are you sure that class B will compile. I tried and it looks for the default contructor in class A.
I believe the rule is that if you specify a contructor with arguments the system will not supply the default constructor and you need to make sure that all references to this class will use the contructor with arguments or you specify a default contructor yourself. And any subclasses need to explicitly call one of the constructors you have specified, using the super(args) statement or else the system will try implicitly call the default constructor and fail. When constructors in the subclass are chained using the this() statement, the last one needs to call the superclass contructor.
Thanks
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cindy Glass:

It is possible that there is NO CASE that you could create an instance of the class without having some parameters supplied. In that case it might be correct to NOT have a no-argument constructor. Anyone trying to create an instance of it with no parameters will get a compile error.


To repeat myself, if you want to make sure that anyone using your class ALWAYS passes parameters that you can use, you leave out the no-argument constructor.
Look at the Color class. It does not have a no-arguement constructor. That is because without having SOME hint what color you want it to be, it just can NOT make an instance.
If you try in YOUR class to create a Color instance with NO parameters you will get a compile error that says that there is no constructor that matches the way you tried to create the instance.
[This message has been edited by Cindy Glass (edited May 16, 2001).]
 
nt novel
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Appreciate all of your replies...
Just to conclude.. one last question with examples...
class A {
A(int i ) {
System.out.println("in A");
}
}
class B extends A {
B(){
super(2);
}
public static void main (String str[]) {
B b = new B();
A a = new A(2);
}
}

class C extends A {
C(int i){
super(i);
}
public static void main (String s[]) {
C c = new C(2);
A a = new A(2);
}
}

class D extends A {
public static void main (String str[]) {
A a = new A(2);
}
}
Now :
What is the reasoning behind classes B and C compile and Run.
Why not class D???
Isnt D similar to the Color example ?
[This message has been edited by nt novel (edited May 16, 2001).]
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In both B and C you took care that any call to an A constructor passed an int.
In class D you did not provide any constructor, therefore the compiler puts in a no-argument constructor for you. The first thing that that default constructor does is to call the super class A using a no-argument constructor. A does not HAVE a no-argument constructor, therefore it will not compile.
 
nt novel
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks vmuch cindy
 
Hey cool! They got a blimp! But I have a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic