Win a copy of Microservices Testing (Live Project) this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

[only Constructor]...

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Only constructors can invoke constructors".
What's the meaning of the above statement ?
The folling code is the example of method invoke constructor??
public class A {
public static void main(String arg[]){
new A();
}
A(){}
}
--------------------------------
thanks for help
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I shall try this..and correct me if I'm wrong.
Here when u say.. new A(), you are actually creating object of type A.
What they mean by constructor invoking constructor..is when you have overloaded constructor in a class..

thanx
 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Swati Bannore :


What they mean by constructor invoking constructor..is when you have overloaded constructor in a class..


And swati, don't u think that when we r implicitely or explicitely invoking construtors in subclasses using super() or super(p1,p2...) then also constructors r invoking constructors?

------------------
azaman
 
chao-long liao
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class A {
public static void main(String arg[]){
new A();
}
A(){}
}
But the code is "method call constructor",and the statement
"only constructor can invoke constructor" is true or false?
-------------------------------
thanks for help
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah..azman...i missed that option...moral of the Story is ..only Constructors can Invoke Constructors...
thx
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It also means that the constructor in the subclasses can also invoke the super class construdtor .
--Farooq
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course it's false. You can invoke a constructor any where you like, including outside of a method like this:
class T{
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course it's false. You can invoke a constructor any where you like, including outside of a method like this:
class T{
String a = new String("Hello Dear Constructor!");
}
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, question is refering to calling a constructor like we call a method...and NOT calling a constructor for creating an Object..u try giving a plain call to constructor from any of the methods...and see what complier tells you...It says..Only Constructors Can Call Constructors..
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think here we are mixing contruvtor invoking (with the key word this() or super()) with object initialization (with the key word new)
--Farooq
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only place you can call a constructor directly is inside another constructor (you can choose between another constructor in the same class, or one in a super-class). The only thing you can do outside a constructor, is create a new class (which will call the constructor, but YOU don't call it).
/Mike
 
reply
    Bookmark Topic Watch Topic
  • New Topic