• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[only Constructor]...

 
chao-long liao
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • 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
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • 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
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
yeah..azman...i missed that option...moral of the Story is ..only Constructors can Invoke Constructors...
thx
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It also means that the constructor in the subclasses can also invoke the super class construdtor .
--Farooq
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic