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

Java constructor

Jacob Michaels
Ranch Hand

Joined: Mar 23, 2003
Posts: 35
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 985

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.


Please ignore post, I have no idea what I am talking about.
Jacob Michaels
Ranch Hand

Joined: Mar 23, 2003
Posts: 35
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

Joined: Jan 30, 2002
Posts: 3451
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


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
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

Joined: Jan 30, 2002
Posts: 3451

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

Joined: Dec 06, 2001
Posts: 3061
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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
So Barry's the Guru on this one. Man I'm sure glad you didn't assess my Developer assignment!
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

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

Joined: Mar 16, 2001
Posts: 985

Great catches Barry.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Jan 30, 2002
Posts: 3451
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java constructor