Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
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
Ron McLeod
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
Sheriffs:
Paul Clapham
Rob Spoor
Junilu Lacar
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Piet Souris
Carey Brown
Bartenders:
Forum:
Beginning Java
Calling one constructor from another
merlin bar
Ranch Hand
Posts: 54
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
public class Test{ public Test(){ System.out.println("Test()"); } public Test( int a ){ System.out.println("Test(" +a +")"); Test();//wont compile } public static void main(String args[]){ Test t = new Test(22); } }
Why does the call to
Test
() int the second ctor cause the compiler to complain? (cannot resolve symbol).
Regards,<br /> merlin_bar
chi Lin
Ranch Hand
Posts: 348
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi,
When call one constructor from another, you use this() or
this(necessary parameter), and it should be the first statement
inside the constructor.
merlin bar
Ranch Hand
Posts: 54
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thats great, thanks!
Regards,<br /> merlin_bar
Gabriel White
Ranch Hand
Posts: 233
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Now why doesn't this work?? It looks properly formatted besides the other constructor call not being the first statement.
public class Test { public Test() { System.out.println("Test()"); } public Test(int a) { Test t = new Test();//added this line is this what you are talking about? System.out.println("Test(" +a +")"); //Test();//wont compile } public static void main(String args[]) { Test t = new Test(22); } }
[ December 15, 2003: Message edited by: Steve Wysocki ]
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
I like...
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
"this" is a keyword. Use is like so:
public class Test { public Test() { System.out.println("Test()"); } public Test(int a) { this(); // invoke the no-arg constructor System.out.println("Test(" +a +")"); } }
Note that a call to this(), if it appears, must be the first line in a constructor -- i.e., you can't print something first, then call this().
[/code]
[Jess in Action]
[AskingGoodQuestions]
Gabriel White
Ranch Hand
Posts: 233
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks E, total brain fart on that one. I just didn't key in on the command that I have used before: this();. Sorry to waste your valuable brain cells on that one.
Thanks
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Value of hexadecimal 0xDeadCafe
Calling a method
can't call another constructor from no-arg constructor
overloading
Overriden exception
More...