Você está na página 1de 4

5/11/13

[only Constructor]... (SCJP forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author

[only Constructor]...
posted 8/19/2001 3:59 PM

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

posted 8/19/2001 4:47 PM

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

[only Constructor]... (SCJP forum at JavaRanch)

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...

posted 8/19/2001 4:58 PM

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, C A, USA.

chao-long liao Ranch Hand Joined: Jul 29, 2001 Posts: 78

posted 8/19/2001 6:37 PM

public class A { public static void main(String arg[]){


2/4

www.coderanch.com/t/208007/java-programmer-SCJP/certification/Constructor

5/11/13

[only Constructor]... (SCJP forum at JavaRanch)

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

posted 8/19/2001 6:50 PM

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

posted 8/19/2001 9:01 PM

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

posted 8/20/2001 7:09 AM

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

posted 8/20/2001 7:10 AM

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

posted 8/20/2001 10:46 AM

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

posted 8/20/2001 1:08 PM

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

[only Constructor]... (SCJP forum at JavaRanch)

Joined: May 08, 2001 Posts: 356

super()) with object initialization (with the key word new) --Farooq

Mikael Jonasson Ranch Hand Joined: May 16, 2001 Posts: 158

posted 8/20/2001 3:34 PM

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.

subject: [only Constructor]...

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 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/208007/java-programmer-SCJP/certification/Constructor

4/4

Você também pode gostar