Java Programming - Declarations and Access Control - Discussion


What will be the output of the program?

public class Test 
    public static void main(String args[])
        class Foo 
            public int i = 3;
        Object o = (Object)new Foo();
        Foo foo = (Foo)o;
        System.out.println("i = " + foo.i);

[A]. i = 3
[B]. Compilation fails.
[C]. i = 5
[D]. A ClassCastException will occur.

Answer: Option A


No answer description available for this question.

Saranya said: (Jul 10, 2010)  
I need a description for this answer.

Gurwinder Singh said: (Sep 27, 2010)  
Here dmd technique is used and no need of casting it. Again that reference address is copied to same inner class reference. That completed the consturction of inner class. So it gave the outpur i=3;.

Note: When reference of Object class is copied to inner class. Type casting is required.

Prinks said: (Nov 14, 2010)  
object is the super class of all the classes.... so kind of type casting is done....

Utkarsh said: (May 10, 2011)  
There is nothing more than Type Cast...

Susant said: (Jun 30, 2011)  
Because of type cast.

Mukta said: (Aug 18, 2011)  
It will work when simply written
Foo foo = new Foo();
no need to write any other statement except this.

but in Question Object class is used.which is superclass of all classes direct or indirect in java including your class in other words father of all it can store any reference of any object.
here its the same
Object o = (Object)new Foo();
once the Foo object reference is stored in o ref is then typecasted in Foo class type.this is needed because we want to access int i variable of foo class.we can not call it directly by using o will give error cannot find symbol.

Alok Gupta said: (Apr 5, 2012)  
In this answer i=3 is right.
Because in the casting manner object can be cast.
And I variable access by the object.

Asim Husain said: (Jul 24, 2012)  
i=3 is correct answer.There is no need of type casting here.Type casting is needed when we subclass has reference of superclass.but we see here we are assigining instance of subclass as

Object o = (Object)new Foo();//we are assignigning instance

type casting will be required as the following condition.

Object o="";
Foo f=new Foo();

Type casting exception will occur in this case.

Rishabh Rastogi said: (Oct 11, 2012)  
Hi friends! Yes, the output will be i=3 i.e. option A is correct.

One more thing.

If we simply write,

* Object o = (Object)new Foo();
* Foo foo = (Foo)o;
Foo foo = new Foo();
System.out.println("i = " + foo.i);

Then also the output is i=3.

Kavita Jalalli said: (Dec 30, 2012)  
Object o = (Object)new Foo();
Foo foo = (Foo)o;

It is equal to
Foo foo=new Foo();

Vivek said: (Feb 1, 2013)  
Object o = (Object)new Foo();

Above line means that the reference variable of Object(class which is the super class of all the classes) is holding the object of sub class Foo. And cast applied to it is optional.

Foo foo = (Foo)o;

Means we are extracting the object of Foo class from the reference of Object class which is o. Now, foo.i is valid and can be printed as 3.

Vinod said: (Feb 8, 2013)  
We can access the class member within the class with object reference or we can access with class reference means with class name also.

Ranjeet Kumar Sahu said: (Apr 24, 2013)  
As we know Object class is the super class of the class hierarchy so the reference of Object class can refer to any of its sub class.

Again we are converting the Object reference to the foo object by typecasting that is possible.

Kripanshu said: (Jun 14, 2013)  
Object is the super class of all the classes so we can put any instance's reference in the Object reference.

So first reference of new Foo() i.e new object of Foo is given in Object reference o;

Now Object reference o is type cast into Foo stored in foo reference;

Now simply accessed the public data member i of class Foo outside the class using (.) operator.

Sunil said: (Jun 16, 2013)  
As object is the superclass for all the defined classes so, in the line.

Object o = (Object)new Foo();

We can assign the downcasting to the same class it doesn't be worried about,it can be done for any class.Now in the next line.

Foo foo = (Foo)o;

There is a downcasting and this kind of casting is legal in java.So it is legal and as o is defined after Foo it stands for the following code.

(Object)new Foo();

So finally there will be a expression as below,

Foo foo=(Foo)(Object)new Foo();

It is valid and properly legal in java code.

Oshin said: (Jul 20, 2013)  
Some time when we typecast superclass object into sub class it will throw a classCastException. When that Exception aries?

Hari said: (Aug 17, 2013)  
This is an example of inner class. To instantiate an inner class, we have to Instantiate outer class first.

Sumeet Sharma said: (Sep 22, 2013)  
1. Declaration of new type inside any method is valid.

2. Since new Object of Foo was casted into Object, it's valid.

3. Since Object O contains "Foo" type of object hence casting from object to Foo is valid.

4. At last we are accessing public member of Foo, which is also valid.

Aniket said: (Oct 25, 2013)  
Object o=(Object)new Foo();-->Here object of Class Foo is Created and Converted it into Super Class Object.

Foo foo=(Foo)o;-->Here the object is Converted into Subclass i.e. Foo and Assign to reference foo.
And finally the Value of i=3 is displayed by foo.i

Anshika said: (Nov 27, 2013)  
Object o=(Object)new Foo();this syntax is written to confuse programmers and check the basics. object is the superclass of all clsses in java.and type casting is done after instantiating an object of class object.

Overlord said: (Aug 1, 2014)  
Can we define a class inside the "public static void main" ?

Bhagya Shri said: (Mar 25, 2015)  
In inner class variables can not be public.

Zin said: (Nov 14, 2016)  
It's using the simple concept of Casting. Here two things are happening. We are explicitly up casting foo object t o object class type reference. And in the next one we are trying to downcast object type instance of an object into custom (user-created class foo). Upcasting is automatic. Don't require to upcast any child class to object class.

But downcasting is mandatory. If you avoid explicit downcasting here, you will get an error.

Vishesh said: (Sep 23, 2017)  
Can we make a class in a method?

Tanmi said: (Jun 12, 2018)  
Please explain me to get it.

Kalyan said: (Oct 6, 2019)  
Can we use nested class in this. I can't understand how it is.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.