Sunday, August 31, 2008

Enum - tricky ones...

I was reading something regarding enums yesterday and found two interesting things which I feel I should share with my readers. These are very simple things for those who already know it but may be surprising for those who don't...

- Enums are not expandable at runtime.
So, suppose you have somethings like
enum Car { HONDA, MERC };
then you cannot add another car say FORD as 3rd constant in the list at runtime. You can have as many references you can to point to the enum constants.

- ; is unnecessary at the end of enum definition! :)
Yeh, this is quite funny and surprising, don't know why java developers didn't do it mandatory. Exam may have questions with multiple code sentences in single line, so look out properly where ; is necessary and where it isn't. And now u know that there is no need of ; after } when the enum definition completes.

Thanks!

Sunday, August 24, 2008

Types of Variables...

Well it is important to know what are the types of variables because there behaviour and their life and death is directly related to their type! There are 6 types of variables and I classify them on basis of 2 things to make you understand better.

- Depending upon what a variable holds they are classified as
1. Primitive Variable
2. Reference Variable

- Depending upon where they are created they are classified as
1. Class variable ( also called static variables)
2. Instance variable
3. Method Parameter
4. Local Variable.

Let's go one by one to understand them. A primitive variable is one which holds the primitive e.g: an int, a char etc. A reference variable is one which holds an object of any class, it can be java API class like String or your wn created class. A class variable is one which is a static variable and inside the class definition. This variable is created when the class loads in the memory and stays there till the class is in the memory. A class variable is not attached to any instance of that class. A instance variable is a non-static member of which is attached to the specific instance of the class. It is reated when a class instance is created and stays in memory untill that instance is in the memory. A method parameter is one which we write in method definition to accept the method arguments the method call is passing to it. This thing will be clear after you see the following example. And lastly a local variable is the one which is declared inside a method body, it is also sometimes called method variable.

Lets take an example to show each type of variable and identify there types..
class City
{
static int name;
Lake l;
int road;
void getDistance(City c )
{
int distance;
}
}
Here,
distance is method variable,
c is method parameter,
road is instance variable,
l is reference variable,
name is class variable
I hope you got it now.. if not fire the doubts in comments! :)

Saturday, August 9, 2008

Some strange facts about Constructors...


Although you would have written many constructors before, I am sure you will find some suprises in following facts about constructors. To start with a constructor is nothing but a piece of code of class with same name as that of class which is called whenever an object is created of that class. Now understand following things one by one...

1.If you don't write a constructor for yourself the compiler will insert one - this is call a default constructor. Constructors can have arguments and there can be a no-argument constructor. Compiler inserted constructor will always be a no-arg constructor.

2. Constructors can take any number of arguments, even var-args i.e.: variable arguments. (More on this in a few days.)

3. Constructors cannot be marked final or static or abstract. Because final, static and abstract keywords are for methods, variables and classes. You can consider that constructors implicitly have final kind of behaviour because they are never inherited! They are not methods beware!

4. Constructors cannot have a return type! This is a very important and perhaps the only criteria to recognise quickly whether the given entity is a method or constructor. A method should have return type but a constructor should never have a return type. A method can have same name as that of the class - its perfectly legal! :)

5. Constructors are never inherited! Again...they aren't methods!! You can remember like this - "Constructors are not passed to the younger generation."

6. Whenever a object has to be created then the constructor of that class has to get called and also the constructors of all its super classes should get called. For example if A is parent of B and B is parent of C then to create an object of C we will call C's constructor which in turn will automatically call the constructors of class B and which inturn will call A's constructor and which inturn will call Object's constructor - as by default class A is child of class Object! Remember that the automatically inserted super class constructor call will be always to the no-arg constructor. If you find it a bit difficult then fire your questions in comments.

7. Constructors can use any access modifier. They can use private, protected and public access modifiers. And if nothing is written they have the default one- package level access. Start thinking why anyone would want to make a private constructor! :)

8. Constructors can be over loaded and hence there can be more than one constructors for a single class. A constructor can call other constructor by the keyword this. A call to another constructor with single argument looks like this(i); .A constructor can even explicitly call its super class constructor by keyword super. A call to parent's constructor with no argument looks like super(); . The first statement of every constructor should be a call to this or super.

9. And here's the most stuning one - Abstract classes have constructors! They are called whenever any object of its subclass is created. So the bottom line is every class has a constructor.

10. Interfaces do not have a constructor. An interface is just a contract, a contract to fulfill the implementation of some methods, remember that.

11. You can have call to both this( ) and super( ) in the same constructor. You can have them one inside other, but not both in any single constructor.

Hoossh.. that's all what I can remember right now.. curse my neurotic RAM! If I find anything else I will update the list. If you have any question, then do comment..as you have seen I reply to every question asked.
C ya,