aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes [only Constructor]... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "[only Constructor]..." Watch "[only Constructor]..." New topic
Author

[only Constructor]...

chao-long liao
Ranch Hand

Joined: Jul 29, 2001
Posts: 78
"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

Joined: Oct 18, 2000
Posts: 201
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


Swati Kale
SCJP
SCWCD
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

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


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
chao-long liao
Ranch Hand

Joined: Jul 29, 2001
Posts: 78
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

Joined: Oct 18, 2000
Posts: 201
yeah..azman...i missed that option...moral of the Story is ..only Constructors can Invoke Constructors...
thx
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
It also means that the constructor in the subclasses can also invoke the super class construdtor .
--Farooq


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
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

Joined: Apr 06, 2001
Posts: 371
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

Joined: Oct 18, 2000
Posts: 201
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

Joined: May 08, 2001
Posts: 356
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

Joined: May 16, 2001
Posts: 158
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [only Constructor]...
 
Similar Threads
Constructors JQ+
Constructor doubts
constructor acces modifiers
Rules for Constructors - K&B Page 315
Constructor Chaining