Java & J2EE Page 4 - Java Classes |
Methods can be named the same as classes. However, a bunch of nerds decided that they should all use the same convention, and that method names should be a lowercase verb, or if you want to add more than one word, then the second, third, and so forth words should have their first letter capitalized. Here are some examples of good method naming conventions: retrieveInfo killBoss eatChickenMcnuggets sleepAtWorkAndStillGetPaid dateAngelinaJolie watchMidgetWrestling slapMisterTThenRunLikeHell Most of the time methods will have a unique name. However, Java does allow for a little thing called overloading, which allows different methods to be named the same and yet still be distinguishable from one another due to method signatures. A method signature consists of its name and parameter types. An example of two methods with the same name and different signatures would be: killChuckNorris(int, int, int, double) killChuckNorris(double, double, double, int) Overloading is a little beyond the scope of this article, however we will discuss it in the future. Constructors: More than Meets the Eye Constructors can be thought of as big beluga construction workers, sitting on a fence and swinging their feet back and forth as they eat from a solid metal sandwich pail, cat-calling and generally going nuts when dames and broads with gams up to here walk by. Aside from belittling women and eating big sammiches, constructors also build objects based on blueprints that you provide. Who'da thunk you'd grow up to be a fancy-schmancy architect type? There are three types of constructors (unless you include copy, which we won't): default, non-default, and cloning. I'll describe each one briefly below: The Good Default Constructor Default constructors are typically used to create the most basic instance possible, which means giving all the members a value such as null or zero, or even an empty string. You can assign values, mind you, but typically you don't or some nerd will yell at you for being a bad programmer. The Bad Non-Default Constructor A non-default constructor is used to create an object with most of its values defined for all of the object's members. If your mother was an object, we could say that she is ugly, she is fat, she is easy, she loves the navy, and she votes Democratic. The Ugly Clone Constructor Settle down all you Star Wars nerds. I know the mere mention of the word Clone stirs you into a Clone Bores frenzy. Clone does what it probably sounds like: replicates an object.
blog comments powered by Disqus |
|
|
|
|
|
|
|