• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help again

 
Brian Ngo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Brian Ngo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively, you could have used the ?: operator within the constructor call:

Isn't that much less complicated than other solutions proposed here?
HTH
Layne
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good example of community refactoring!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic