Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do nested classes in java differ from nested classes in C# in the following aspect?

Tags:

java

c#

oop

I found a couple of posts resembling this, but couldn't find an answer that trenchantly explains this.. I have performed nesting of a class, i.e class 'inside' is present in class 'outside' and made an attempt to instantiate the inner class, and this is the scenario that i came across through

In case of C# :

    class outside
    {
        public class inside
        {
            public void print()
            {
                System.Console.WriteLine("I am inside");
            }
        }
    }
    class Program
    {
        public static void Main(string[] args)
        {
            /* here i am trying to instantiate the inner class 'inside' */
            outside.inside obj = new outside.inside();
            obj.print();
            Console.ReadKey();
        }
    }

Output:

I am inside So, the above is working fine..But,
In case of Java :

class outside
{
    public class inside
    {
        public void print()
        {
            System.out.println("I am inside");
        }
    }
}
class Demo
{
    public static void main(String[] args)
    {
        /* here i am trying to instantiate the class 'inside' */
        outside.inside obj=new outside.inside();
        obj.print();
    }
} 

Output:

Demo.java:16: error: an enclosing instance that contains outside.inside is required...

This is the situation in case of Java.. What this error is ?

Does it mean that the outer class 'outside' cannot access inner class 'inside' using the dot operator because it is not static? If so, then why the same doesn't generate a compilation error in c# ?

like image 378
Srinivas Cheruku Avatar asked Dec 12 '22 14:12

Srinivas Cheruku


1 Answers

The problem is that the way you have declared classes in Java, the inner class has an implicit reference to an instance of the outer class. Hence the error message: "error: an enclosing instance that contains outside.inside is required". This means you need to:

Outside out = new Outside();
Outside.Inside in = out.new Inside();

In general, this pattern is used in Java in situations where it makes no sense for an instance of the inner class to exist without an instance of the outer class; and note that the inner class instance will have access to all of the outer class instance's variables, even private ones. But in general, such classes are generally private.

In order for this to disappear, you must make the inner class static. And then you will be able to do:

Outside.Inside in = new Outside.Inside();

Edit: completent on what static means in Java: static whatevers (variables, classes, methods) in Java are accessible at the class and instance level. You can access a static variable from a non static method for instance (this also means you can call static methods from an instance of a class!); but a static method CANNOT access a non static variable, nor invoke an "instance only" method etc.

Also, a "top level" class cannot be static, because it makes no sense.

like image 76
fge Avatar answered Apr 27 '23 04:04

fge