What is a static nested class? [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java: Static vs non static inner class
What is a static nested class?
What is the difference between static and non-static nested classes?

A static inner class is a class nested inside another class that has the static modifier. It's pretty much identical to a top-level class, except it has access to the private members of the class it's defined inside of.
class Outer {
private static int x;
static class Inner1 {
}
class Inner2 {
}
}
Class Inner1 is a static inner class. Class Inner2 is an inner class that's not static. The difference between the two is that instances of the non-static inner class are permanently attached to an instance of Outer -- you can't create an Inner2 without an Outer. You can create Inner1 object independently, though.
Code in Outer, Inner1 and Inner2 can all access x; no other code will be allowed to.

Related

Static class member of normal class [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java: Static vs non static inner class
What is a static nested class?
What is the difference between static and non-static nested classes?
A static inner class is a class nested inside another class that has the static modifier. It's pretty much identical to a top-level class, except it has access to the private members of the class it's defined inside of.
class Outer {
private static int x;
static class Inner1 {
}
class Inner2 {
}
}
Class Inner1 is a static inner class. Class Inner2 is an inner class that's not static. The difference between the two is that instances of the non-static inner class are permanently attached to an instance of Outer -- you can't create an Inner2 without an Outer. You can create Inner1 object independently, though.
Code in Outer, Inner1 and Inner2 can all access x; no other code will be allowed to.

How non static inner class can be called using outer class name

public class InnerTest {
public static void main(String arg[]) {
A.B.print();
}
}
class A {
static class B {
static void print() {
System.out.println("Hello");
}
}
}
How can i call static class B using class name A although class A is not static
This is not related to the class to be static or not, it is related the static keyword in the method.
take a look about How static keyword exactly works in Java? also read this article Java – Static Class, Block, Methods and Variables
One more aspect how to explain this:
class itself is not static or non static it is just a class.
You anyway can use static keyword only with class members. If you would try to declare InnerTest as static you would have an error that might look like this (so assuming it is not static nested class to some other class)
Illegal modifier for the class InnerTest; only public, abstract &
final are permitted
static nested class can be used as in question because it does not require access to any instance member of InnerTest. In other words it can access static members and only those.
If it needs access to non static members then it can not be static and the way to call would be like new InnerTest().new B().
The static keyword is used to modify a member of a class. When a member is modified with static, it can be accessed directly using the enclosing class' name, instead of using an instance of the enclosing class like you would with a non-static member.
The inner class Inner below is a member of the class Outer:
class Outer {
static class Inner {}
}
Therefore, Inner can be accessed like this:
Outer.Inner
Outer don't need to/cannot be modified by static because it is not a member of a class. It is a class existing in the global scope. To access it, you just write its name - Outer. It does not make sense for it to be non-static because it has no enclosing class. If it were non-static, how are you supposed to access it?
To use the correct terminology, class B is not an inner class; it is a static nested class. See https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html for the definitions of the various types of nested class.
The important keyword is the static keyword in front of the definition of class B. It does not matter whether class A is static or not. In fact, it wouldn't make sense to put static in front of class A's definition.
Because class B is declared static, it doesn't keep a reference to an instance of class A.

difference between extends inner class and extends nested class? [duplicate]

This question already has answers here:
Java: Non-static nested classes and instance.super()
(5 answers)
Closed 6 years ago.
class OuterClass
{
static class InnerClassOne
{
//Class as a static member
}
class InnerClassTwo
{
//Class as a non-static member
}
}
class AnotherClassOne extends OuterClass.InnerClassOne
{
}
class AnotherClassTwo extends OuterClass.InnerClassTwo
{
public AnotherClassTwo()
{
new OuterClass().super(); //accessing super class constructor through OuterClass instance
}
}
i have theses classes, why when extends from nested class we do not call outer class constructor , but when extends from inner class should call outer constructor through outer object, so what is the difference and why??
The reason is simple. To access a static property you don't need to create an object i.e. you don't need to instantiate the class.
But if you want to access a non static property you will first need to create an object of that class and then use it.
So here in your case when you want to extend InnerClassTwo(which is an inner class and non static) you will have to associate it with the constructor of the outer class AnotherClassTwo, as it can be thought of as a property of that outer class.

Java: reference outer class in nested static class [duplicate]

This question already has answers here:
Java inner class and static nested class
(28 answers)
Closed 7 years ago.
I have a class structure like this:
public class OuterClass {
private static class InnerClass {
public void someMethod() {
OtherClass.otherMethod(<???>);
}
}
which refers to a static method of some other class OtherClass:
public class OtherClass {
public static void otherMethod(OuterClass) {
....
}
}
I am trying to figure out what to put in place of the <???>. How do I refer to the instance of the outer class from within the inner static class? What I would like to do is to refer to the implicit this of the OuterClass.
You obviously need an object of OuterClass type:
public void someMethod() {
OuterClass oc = new OuterClass();
OtherClass.otherMethod(oc);
}
In case that your inner class is not static, then you could do:
//remove static here
private class InnerClass {
public void someMethod() {
OtherClass.otherMethod(OuterClass.this);
}
}
You should know the different between nested classes - static and non static. Static nested classes are simply classes like every other, just defined within other class (usually because of encapsulation principle). Inner static class instances have no knowledge of outer class instance.
Nested inner classes (non static) mandate that an object of the inner class exist within an instance of the outer class. That's why you can access it via OuterClass.this.
The simpliest way is to pass an instance of the outerClass in the constructor or in the method since the innerClass don't know this class.
like this:
public void someMethod(OuterClass outerClass) {
OtherClass.otherMethod(outerClass.myMethod());
}

Why a non-static inner-class cannot have static members (fields and methods)? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why cant we have static method in an inner class?
I know that the creation of a non-static inner-class object requires an outer-class object and the created non-static inner-class object automatically have a hidden reference to the object of the outer-class. But why can't a non-static inner-class have static members? The Java designer just have to disallow the access of non-static outer-class fields inside a static method of the inner-class, it would make more sense, non?
If it does not make sense to have static members in an inner class, why inner class can inherit static members by inheriting a class who has static members?
I read this post too. As is mentioned:
Inner classes may inherit static members that are not compile-time
constants even though they may not declare them. Nested classes that
are not inner classes may declare static members freely, in accordance
with the usual rules of the Java programming language.
Is it a convention?
Here is my code:
public class OuterClass {
private int outerClassField;
public void doSomethingOuterClass() {
outerClassField = 1;
}
public static void doSomethingStaticOuterClass() {
// outerClassField = 2; // Error: Because static method cannot access an specific object's field
}
public class InnerClass extends ClassWithStaticField {
// Error: Why a non-static inner class cannot have static fields ?
// public static int innerClassStaticField = 1;
public void doSomethingInnerClass() {
outerClassField = 3;
staticField = 1;
}
// Error: Why a non-static inner class cannot have static methods ?
// public static void doSomethingStaticInnerClass() {
// outerClassField = 4;
// }
}
public static void main(final String[] args) {
// If it does not make sense to have static members in an inner class, why inner class can inherit statis members by inheriting a class who has static
// members?
OuterClass.InnerClass.staticField = 1;
OuterClass.InnerClass.staticMethod();
}
}
class ClassWithStaticField {
public static int staticField;
public static void staticMethod() {
};
}
1. An object of a Non-static inner class is associated with an instance/object of its Outer Class (ie enclosing class).
2. The entire body of a Non-static inner class is Not within a static scope, and therefore you can't have static members in there.
3. Instance variables and methods in the non-static inner class are relative to an instance of the enclosing class, so being related to an object, static won't hold true for them (ie inner class),
4. When we create an Instance of non-static inner class, we Need an Object of Outer enclosing class. The innernon-static class has an implicit reference to it outer enclosing class.
Eg:
Outer o = new Outer();
Outer.Inner i = o.new Inner();
Technically there I don't know of any reason why the language restricts the use of static elements for inner classes.
A nonstatic inner class can be emulated by using a normal class that takes the (formerly) enclosing instance as an argument to the constructor. Of course there are many little differences when it comes to visibility rules an visibility scopes.
I presume it was a language design decision, mostly because statics in non-static inner classes are confusing and non-intuitive to access (Outer.Inner.StaticMember).
There is no point of providing a static method inside a non static inner class.
You could use a non static method inside the outer class instead ?

Categories

Resources