aspose file tools*
The moose likes Beginning Java and the fly likes need help again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need help again" Watch "need help again" New topic
Author

need help again

Brian Ngo
Greenhorn

Joined: Mar 03, 2003
Posts: 22
ok the problem is i have done this piece of code and every thing seems ok till this part, i do not understand the error message, it says call to this must be first statement in constructor. what does it means
the following is what i have done
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
well theres a few problems with this, and this is the main problem
calls to this/super can only be made in constructors, and it has to be the first line of code, you tried to do it based on what your if returned to you.
to get around this you could do the following

problem with that is contructors dont return any thing, so you'll get something like this printed out "Test@1".
now to get around that you can print your output in the constructor.


now that will return the result you're looking for, alternatively you could use constructors for what they're intended for, to construct/initialiase your objects, and put your calculations into methods
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Consider this:
This constructor will do the following:
1. call super()
2. Make a new Test object with the Test(double, double) constructor.
3. Throw the newly created object away
All resulting in not what you want.
Consider using a factory method:

You will also need to add a String toString() to your class. This method should return a String representation of the object.
[ March 19, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Brian Ngo
Greenhorn

Joined: Mar 03, 2003
Posts: 22
ok i get it now thanks as for my print statement it is not valid if i do it this way right.


can i use a static method to print both member variables?
[ March 19, 2003: Message edited by: Brian Ngo ]
Brian Ngo
Greenhorn

Joined: Mar 03, 2003
Posts: 22
forget my last post i get the result i wanted
and i now know that i can't use a static method to print out the both members as 1 of then is non static
thanks for all the help
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
And the other bit of information to walk away with is:
The very first thing every constructor does is to invoke a superclass constructor or another constructor in the same class. This invocation must be the very first thing to occur. So, you don't get to perform any logic to provide for alternate constructor chaining paths.
Note that if you don't specify a superclass constructor invocation or the invocation of another constructor in the same class, the compiler slips one in for you - it provides a call to the no-argument superclass constructor. (Nevermind what Object's constructors do.)


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Alternatively, you could have used the ?: operator within the constructor call:

Isn't that much less complicated than other solutions proposed here?
HTH
Layne


Java API Documentation
The Java Tutorial
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
A good example of community refactoring!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help again