Escolar Documentos
Profissional Documentos
Cultura Documentos
Java FAQ
Recent Topics
Register / Login
Author
[only Constructor]...
posted 8/19/2001 3:59 PM
"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..
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 .
p u b l i cc l a s sD e m o {
1/4
www.coderanch.com/t/208007/java-programmer-SCJP/certification/Constructor
5/11/13
0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 .
i n ti , j , k ; D e m o ( ) { i=1 0 ; j=2 0 ; } D e m o ( i n tm ) { t h i s ( ) ;/ /c a l lt oa b o v ec o n s t r u c t o r k=m ; S y s t e m . o u t . p r i n t l n ( " i="+ i+ "j=" + j + "k =" + k ) ; / /t h i ss h a l lp r i n tt h ev a l u e so fi , ja n dk } p u b l i cv o i dm e t h o d ( ) { t h i s ( ) ;/ /t h i ss h a l lg i v ey o ue r r o r S y s t e m . o u t . p r i n t l n ( " C a nIc a l lc o n s t r u c t o r . . ? " ) ; } p u b l i cs t a t i cv o i dm a i n ( S t r i n ga r g s [ ] ) { D e m oo b j=n e wD e m o ( 3 0 ) ; o b j . m e t h o d ( ) ; } }
thanx
Swati Kale SC JP SC WC D Ashik Uzzaman Ranch Hand Joined: Jul 05, 2001 Posts: 2370
I like...
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, C A, USA.
www.coderanch.com/t/208007/java-programmer-SCJP/certification/Constructor
5/11/13
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 C ertified Programmer for Java 2 Platform<br />Oracle8i C ertified 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..
I think here we are mixing contruvtor invoking (with the key word this() or
3/4
www.coderanch.com/t/208007/java-programmer-SCJP/certification/Constructor
5/11/13
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
Similar Threads Rules for Constructors - K&B Page 315 Constructor doubts Constructors JQ+ Constructor Chaining constructor acces modifiers
All times above are in your local time zone & format.T he current ranch time (not your local time) is May 11, 2013 05:16:06 .
www.coderanch.com/t/208007/java-programmer-SCJP/certification/Constructor
4/4