As I was reading my Java SCJP book tonight, it kept mentioning things that I should definitely learn and read a few times. I often find that writing things down helps me to memorize bits of information so I was going to write down some of the more important points which I didn't already know. Then I thought I might as well do a blog entry on it, then not only would I benefit but I would also know where to retrieve the information again and it might help other people studying who stumble onto my blog.
I also thought that if I blog about something that will be on the exam I will be still learning stuff for my exam rather than just blogging about something interesting. This way I will be learning stuff for my exam and blogging.
It might not be that interesting to readers of my blog who aren't studying for the SCJP Java 5 exam or those who have already done the exam but I'm sure there will be a few facts here that you will have forgotten so give it a read.
I was reading about Constructors this evening, now this is a fairly straight forward topic or so I thought because there are quite a few tricky little rules, which I certainly raised an eye brow to a few of them.
The information below isn't a comprehensive guide on Constuctors but it's a sort of cheat sheet of the more tricky or must know stuff about Constructors. In my mind it's a sort of cheat sheet of quick facts about constructors, random facts for some quick revision.
1. Key points about constructors.
- They have no return type
- Their names must exactly match the class name (yes it is case sensitive)
2. Every class, including Abstract classes must have a constructor but you don't always have to type one, if you don't type any constructor then the JVM will automatically give you a no arg's constructor.
3. Constructors can use any access modifier which includes private.
4. You can have methods with the same name as a constructor. It is legal to have a method the same name as the class and the constructor
5. If you don't type in a constructor the compiler will automatically add one in for you, this will always be a no argument constructor.
6. If you have typed in a constructor the compiler will NOT add in the default no arg constructor, so don't try and call it.
7. Every constructors first line of code is either super() or this(). If this isn't the first line of code the compiler will automatically add this in
8. One of the constructors will always call the super class constructor (e.g. super()) if you call another constructor in the class then the super() will be prosponed until it go's into constructor that doesn't call another constructor.
9. You cannot call any non static variables/methods until after the super() constructor has run. You can use static variables/methods before this.
10. Interfaces do not have constructors
11. You can only invoke a constructor from inside another constructor
12. Abstract class constructors are called when the concrete subclass is initialized
13. The default constructor includes a no-arg call to the super constructor
14. Constructors are never inherited
15. Constructor cannot be overwritten but they are often overloaded.
The Constructor Exam gotcha's
Constructors cannot have return types, so if you see one with void, int, String etc, it isn't a constructor it's just a method. This is often used with tricky overwriting questions. They can be sneakily name the same
Constructors MUST be the same name as the class. They are also case sensitive so watch out.
If you have typed in a constructor the compiler will NOT add in the default no arg constructor, so don't try and call it. This is a sneaky exam trick, they show you a class with a compiler that has a constructor that takes some arguments and then they call a no arg constructor, bang error.
You can only invoke a constructor from inside another constructor. If you see any method calling a constructor you know it won't work.
The default constructor includes a no-arg call to the super constructor, this might be cause a problem if the super class doesn't have a no arg constructor, which means you will have to change this and add your own super constructor with parameters. So if you superclass doesn't have a no-arg constructor you cannot use the default constructor added by the compiler