File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question" Watch "Question" New topic
Author

Question

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Hello Ranchers ,

Please help me to understand these questions

1. What is shadowing of variables?

2. class Mixer
{
Mixer()
{
}

Mixer(Mixer m)
{
m1 =m;
}
Mixer m1;

public static void main(String a[])
{
Mixer m2=new Mixer();
Mixer m3= new Mixer(m2);
Mixer m4= m3.m1;
Mixer m5= m2.m1;

m3.go();
m4.go();
m5.go();
}

void go(
SOP("hi");
}
}

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?
Where to initialised m2 object's so prg complies fine.

refer pg 261 in kb
---------------------------------------------------

3. class Fizz
{
int x=5;
public static void main(String a[])
{
final Fizz f1= new Fizz();
Fizz f2= new Fizz();

Fizz f3= FizzSwitch(f1,f2);
SOP((f1==f3)+" "+(f1.x==f3.x));
}

static Fizz FizzSwitch(Fizz x, Fizz y)
{
final Fizz z=x;
z.x=6;
return z;
}
}

Answer: true true
Can some expalin why its printing true?

----------------------------------------------
4. class Bigger
{
public static void main(String [] a)
{
//insert code
}
class Better
{
enum FASTER(Higher,Longer);
}

Answer : Better.Faster f= Better.Faster.Higher
Better.Faster f2; f2 = Better.Faster.Longer

Can someone please expain me and state this with some differnet example.


Thanks
Dinesh




Thanks<br />Dinesh
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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 ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355


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.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355


Answer: true true
Can some expalin why its printing true?


For your Q3, try to understand the program first. Atleast with the explanation i had given for your previous query.

Also have a look at these URLs for further understanding.

  • Pass by - Story
  • Pass by References for objects holds good really?


  • After going through this, kindly go through the code patienly. I am sure you WILL definitely get the reason
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3355


    Answer : Better.Faster f= Better.Faster.Higher
    Better.Faster f2; f2 = Better.Faster.Longer

    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 ]
    Dinesh Tahiliani
    Ranch Hand

    Joined: Aug 06, 2007
    Posts: 486
    public class Mixer
    {
    Mixer()
    {}

    Mixer(Mixer m)
    {
    m1 =m;
    }

    Mixer m1;

    public static void main(String a[])
    {

    Mixer m2=new Mixer();
    Mixer m3= new Mixer(m2); // m3.m1=m2//
    Mixer m4= m3.m1;
    Mixer m5= m2.m1;

    m3.go();
    m4.go();
    m5.go();
    }

    void go()
    { System.out.println("hi");}
    }

    What is Mixer m1 here is it the local variable or instance variable?

    Can someone please right the correct code and explain me?

    Code should print "hi" three times.

    Thanks
    Dinesh
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14352
        
      22

    When you post one or more questions, please use a meaningful subject line instead of just "Question" or "Hello".


    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3355

    Originally posted by Dinesh Tahiliani:

    What is Mixer m1 here is it the local variable or instance variable?



    What do you think?


    Can someone please right the correct code and explain me?

    Code should print "hi" three times.


    No one can write the code for your homework. Please try something of your own and when you are stuck up somewhere, we can discuss and help you out to resolve the issue.

    Please spend sometime in reading How to ask questions in ranch.
    [ January 02, 2008: Message edited by: Raghavan Muthu ]
    Rahul Nair
    Ranch Hand

    Joined: Dec 01, 2007
    Posts: 138


    This is one of the option in the option list. I just want to know Why this wrong option ???

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Question