This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java constructor

 
Jacob Michaels
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This example came from study guide that I am using. I can't understand how the very last println(i) is 1
When I typed this code out it would not even compile, but the probelm stated to assume that the code compiled correctly. I understand that after the main method is envoked: Entering the try{
i = i++; //i remains 0
The next line is confusing me:
Test123 test1234 = new Test123();
I look at this code and see a constructor, nothing more. How is this line of code running the validate method that is defined public class test123(). I see the line:
this.validate(boolean1, boolean2)
But my question is how is the constructor:
Test123 test1234 = new Test123() in the try{}
running this method?
public class Test123
{
int i = 0;
public Test123()
{
Boolean boolean1 = new Boolean(true);
Boolean boolean2 = new Boolean(true);
this.validate(boolean1, boolean2);
}
public Integer validate(Boolean boolean1, Boolean boolean2)
{
assert boolean1.equals(boolean2) : "Assert Failed";
i = i + 1;
return new Integer(0);
}
public static void main(String[] args)
{
try
{
i = i++;
Test123 test1234 = new Test123();
System.out.println("No Assertion Error");
} //end try
catch(AssertionError ae)
{
System.out.println("Asssertion error in 123");
i = ++1;
}
System.out.println(i);
}
}
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, the

isn't the constructor. This simply calls the constructor which is

As you can see, the constructor then calls the validate() method. So to answer your question, this is how the validate() method is run.
HTH
Layne
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I typed this code out it would not even compile

I would suggest that you get the code to compile, than put some printlns in. There is no better way of learning how something works than running the code.
But to answer your question: the ctor is able to validate the input, because it is calling the "validate" method.
 
Jacob Michaels
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that:
Test123 test1234 = new Test123();
creates a Test123 object called test1234
maybe it is the same thing as saying:
test1234.Test123();
not sure
Thanks for the quick response and I tried to get the code to compile but I keep getting this error about the word assert.
Thanks
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't get it to compile either. It has to do with the assert statement. According to this article, that is the proper syntax. I must admit my ignorance on Java assertions at the moment. Does the 1.4.1 compiler recognize assertions? I just get a warning that as of release 1.4 assert is a keyword. The next compiler complaint is the irritating ';' expected bull at the assert statement. Maybe some of you gurus can comment on this.
Michael Morris
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, the IDE doesn't work. Had to correct his uppercase B's in boolean but that assert ; is stupid. I have no idea about that one. I read somewhere that you had to declare the assert in order to use it. But my JB8 didn't give me any problems with the reserved word "assert" I thought the correct syntax was:
assert assertion/boolean expression [:comments];
I tried that and it didn't work either.
btw, I thought you were the guru Mike. UBB to you
[ March 28, 2003: Message edited by: Steve Wysocki ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

btw, I thought you were the guru Mike. UBB to you

I think I am too after half a bottle of Mezcal I would advise not using the U word in vain else some of his high priests sacrifice one of your lengthy posts that you have put much time and thought into to the U been bit god.
Michael Morris
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jacob Michaels:
I thought that:
Test123 test1234 = new Test123();
creates a Test123 object called test1234

It does! After it creates the object, it also initializes it by calling the constructor, which is basically the same as test1234.Test123(). However, calling the constructor directly like this is not allowed, afaik.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. compile with javac -source 1.4
2. fix the declaration of i
3. ++1 <- preincrement one ???
I tell you, This program is just made for SCJP freaks! Wow!
Look there's a lovely i = i++!
And a Constructor calling a method too, Whoo!
Boy o boy...
And it catches an AssertionError, hey man...a gem.
[ March 28, 2003: Message edited by: Barry Gaunt ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Barry's the Guru on this one. Man I'm sure glad you didn't assess my Developer assignment!
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And a Constructor calling a method too, Whoo!

What's wrong with calling a private method to verify state when you have multiple constructors? For example:

Michael Morris
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great catches Barry.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I tell you, This program is just made for SCJP freaks! Wow!
Look there's a lovely i = i++!
And a Constructor calling a method too, Whoo!
Boy o boy...
And it catches an AssertionError, hey man...a gem.

.. Not to mention the choice of variable names, such as boolean1, boolean2, test1234. Who is the author of this code? He should be sent to do hard labor in Alaska.
Eugene.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael said:
What's wrong with calling a private method to verify state when you have multiple constructors?

The method in question is public. So if the class was extended and the method was overridden you could have problems initializing the state of an object of such a subclass. (don't blame me, blame Kathy )
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with that, I thought that you meant never to call a method from a constructor.
Michael Morris
[ March 28, 2003: Message edited by: Michael Morris ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic