As already told in your previous thread, please make sure you post the queries in a separate thread. Also please post the code using "Code" tags.
1. What is shadowing of variables?
A variable is said to be shadowed (being masked or hidden) when the name of one variable seems to be of the other in different scope.
For example, if you declare a variable in a method which has the same name of the class level variable, the method level variable is said to shadow the class level variable.
Look at this example.
If you run the above program as such, you would get the output as
There is nothing special here. A normal execution flow of accessing a class level variable inside the static method. Whereas when you run this program by uncommenting the "Line 1", the output would be
Here the variable "integerVar" inside the main() method shadows the class level variable "integerVar".
It happens in inheritance as well. In Inheritance, a subclass can shadow the variable of superclass. You can access the superclass specific variable inside the subclass using keyword "super", like "super.<superclass_varName>" wherever you need to access the variable of superclass which is shadowed.
Does that help? [ December 29, 2007: Message edited by: Raghavan Muthu ]
Answer: hi,hi followed by an expection The m2 object's m1 instance variable is never initialised, so when m5 tries to use it throws expection.
Can anyone please explain me wht is mentioned?
Well. This requires a bit of patience in understanding the objects being created and references.
If you patiently look at the objects being instantiated and assigned at each step, you would get to know that the reference variable "m5" is actually assigned the "m1" reference of the object referred by "m2".
When you look at the reference variable "m3", it is instantiated by invoking the one-arg constructor "new Mixer(m2)" by passing the newly instantiated object through its reference variable "m2". It invokes the one-arg constructor and assigns the reference variable "m2" to the "m1" of the object being created. In such case, m3.m1=m2..
Of course, "m2" holds the very first object being instantiated. But it was instantiated by simply invoking the no argument constructor because of invoking simply "new Mixer()". This does NOT assign any object to the reference variable "m1" for the new object being created. Instead it simply creates a new object for class Mixer. As "m1" is not initially set with anything and inside constructor as well, being a reference variable it gets its default value "NULL".
You can see the same by looking at the empty implementation of no-arg constructor "Mixer()". In such case, the reference variable "m1" is NOT assigned to any object thereby it IS NULL.
Just for your understanding please see the lines below.
That's when when you invoke the go() method with reference "m5" which holds the same reference as "m2.m1" which is actually NULL, you get an exception. That is a famous NullPointerException.
Where to initialised m2 object's so prg complies fine.
The program would compile fine as such it does NOT find anything abnormal with respect to the syntax. But it is a logical error and it will definitely blow up at the runtime.
Can someone please expain me and state this with some differnet example
What do you understand from that program? Have you read about "enums" and the way they work?
You seem to be simply posting the queries without some homework. Please do your homework first. Then come up with the specific doubts you have or the piece of code you do NOT understand. . There are many people here to help us out.
I am off to bed. See you tomorrow [ December 29, 2007: Message edited by: Raghavan Muthu ]