Accessing static variable Country with the help of outer class name by calling the inner static class
Here in this code I kind of understood that we are using class name along with dot operator as both the inner class and variable inside it are static .
1: What if the inner class is not static and variable is also not static ? Then how do we access variable Country & print it from main ?
2: What if the inner class is static and variable is not static ? Then how do we access variable Country & print it from main ?
2nd possibility cannot happen right as a static variable inside a inner class requires that inner class to be static also?
I'm going to use the word "accessible" a bunch in this post, but it's important to remember that, generally speaking, access to class members is controlled through something called access modifiers. Everything I'm about to say assumes that there is no access modifier preventing access to some member of a class.
__________
Straightening up the formatting a bit from the original post:
Java:
class Man
{
int x=0;
static int y = 0;
class Test {
String country = "INDIA";
}
}
public class Main
{
public static void main(String[] args)
{
Man obj = new Man ();
System.out.println(obj.Test.country);
}
}
A class definition is never a member of an instance of an outer class. Here, the class Man.Test exists, but an instance of Man does not contain it, as was attempted with obj.Test.
class Man
{
int x=0;
static int y = 0;
static class Test
{
static String country = "INDIA" ;
}
}
public class Main
{
public static void main(String[] args)
{
System.out.println(Man.Test.country);
}
}
Here, class Man.Test has a static field country, which is accessible via Man.Test.country because it is static. A static field, by definition, does not exist in the context of an instance of a class, but rather in the context of the class itself. As long as the class is defined, its static members are accessible without there being any instances of that class.
If the field is not static, then an instance of the class is required. If the field is static, then there need not be an instance of the class.
A static class, on the other hand, is a class that does not depend on an instance of its outer class. There can be instances of static classes. When inner classes come into play, the fields and members of the specific instance of some outer class are exposed to instances of its non-static inner classes.
If neither the inner class nor its field are static, then an instance of the inner class is needed in order to access the field. This would also require an instance of the outer class, as demonstrated in this post:
Java:
class Man
{
int x=0;
static int y = 0;
class Test
{
String country = "INDIA" ;
}
}
public class Main
{
public static void main(String[] args)
{
Man.Test t = new Man(). new Test();
System.out.println(t.country);
}
}
If the inner class is static but its field is not, then the field is only accessible from instances of the inner class. For example:
Java:
class Man
{
int x=0;
static int y = 0;
static class Test
{
String country = "INDIA" ;
}
}
public class Main
{
public static void main(String[] args)
{
Man.Test t = new Man.Test();
System.out.println(t.country);
}
}
There is no such requirement. static on a field means it does not belong to an instance of its class. static on a class means that instances of that class do not belong to instances of its containing class. No more, no less.
In the C# programming language, which is very similar to Java, static classes work differently. C# static classes cannot be instantiated, and as such cannot contain any non-static members.
In neither language is there any restriction on whether a class is allowed to contain static members. All classes in both languages may contain static members.
Man.Test identifies the inner class. There is a class Man that contains a class Test, hence the notation.
new Man() produces an instance of the outer class. Invoking .new Test() on that instance (the dot is important) in turn produces an instance of the inner class. This chain of events is necessary when both classes are non-static.
If the inner class is static, however, then instances of it can be directly created via new Man.Test() regardless of whether or not Man is static. Instances of an inner class are only pertinent to instances of the outer class if the inner class is non-static.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.