This is my main class:
import java.util.ArrayList;
public class MainClass {
public static void main(String[] args){
ArrayList<SecondClass.InnerClass> list=new ArrayList<SecondClass.InnerClass>();
list.add(new SecondClass.InnerClass()); //error here (read below)
}
}
Here is the second class:
public class SecondClass {
public class InnerClass{
}
}
At MainClass, at list.add, I get this error:
No enclosing instance of type SecondClass is accessible. Must qualify
the allocation with an enclosing instance of type SecondClass (e.g.
x.new A() where x is an
instance of SecondClass).
I need to have InnerClass non-static because InnerClass needs to make a static reference to a non-static method. How can I add elements in the ArrayList?
I think you need:
new SecondClass().new InnerClass()
I'd do some reading up on nested classes, and in particular the differences between static and non-static nested classes.
If you choose to make InnerClass a static nested class, note the following:
A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
This means you don't need an instance of SecondClass in order to make an instance of InnerClass - You can instantiate it as you do at the moment.
If however you make InnerClass a non-static nested class (I believe these are sometimes referred to as inner classes, but double check this terminology), you need to create an instance of SecondClass in order to create an instance of InnerClass:
new SecondClass().new InnerClass()
When you want to use an innerClass you must create an instance of the class it contain innerclass. After that you'll use it. Example how to use an innerclass following the tutorial of Oracle:
SecondClass sc=new SecondClass();
SecondClass.InnerClass in=sc.new InnerClass();
And you can see detail here: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Make InnerClass static
public class SecondClass {
public static class InnerClass{
}
}
Related
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.
I know how to make an instance of an inner class. But I want to know why we can not do that in the following way:
class outerclass{
public static void main(String[] args){
innerclass in=new innerclass();
}
class innerclass{
}
}
If I do this then I get the following error:
No enclosing instance of type outerclass is accessible. Must qualify the allocation with an enclosing instance of type outerclass (e.g. x.new A() where x is an instance of outerclass).
Why?
class Demo{
public static void main(String[] args){
System.out.println(innerclass.a);
}
static class innerclass{
static int a=1;
}
}
Gives the output 1.
See here while making the inner class as static You can easily access in your outer class,In order to create an instance of the Nested class you must reference it by prefixing it with the Outer class name, like this:
Outer.Nested instance = new Outer.Nested();
Non-static Nested Classes (Inner Classes)
Non-static nested classes in Java are also called inner classes. Inner classes are associated with an instance of the enclosing class. Thus, you must first create an instance of the enclosing class to create an instance of an inner class. Here is an example inner class definition:
public class Outer {
public class Inner {
}
}
Here is how you create an instance of the Inner class:
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
Notice how you put new after the reference to the outer class in order to create an instance of the inner class.
Non-static nested classes (inner classes) have access to the fields of the enclosing class, even if they are declared private. Here is an example of that:
public class Outer {
private String text = "I am private!";
public class Inner {
public void printText() {
System.out.println(text);
}
}
}
Your innerclass is not static. This means it must have a reference to the outerclass. main is static and has no implicit outerclass.
The simple solution is to make your inner class a static nested class.
You must either make your inner class static (as already mentioned) or create your inner class from a non-static context, e.g. from a non-static method.
I.e. either this:
class outerclass{
void myMethod() {
innerclass in = new innerclass();
}
class innerclass{
}
}
or this
class outerclass{
public static void main(String[] args){
innerclass in=new innerclass();
}
static class innerclass{
}
}
outerclass thats encapsulates innerclass is not instantiated, hence, calling innerclass directly would throw an error since there is no outerclass to attach innerclass.
Therefore as suggested by the previous answers, making innerclass static would resolve the problem, allowing access to the innerclass without instantiation.
There are lot of existing answers with regards to this topic. A quick google brings this up.
Java - No enclosing instance of type Foo is accessible
Just looking for a confirmation.
public class Indeed{
public static class Inner implements Runnable{
public void run()
{
System.out.println("Indeed");
}
}
public static void main (String []args)
{
Indeed.Inner inner = new Indeed.Inner();
inner.run();
}
}
As you can see in the code above, I can declare public void run() without declaring it static. I guess it's implicitly done. Isn't it?
One more question related: Why I cannot use the method run as following: Indeed.Inner.run(); it is static after all, there should not be any need of instantiating the inner member at all? ( I know I am wrong as it does not compile if I do that, however I would like to know why).
Thanks in advance.
As you can see in the code above, I can declare public void run() without declaring it static. I guess it's implicitly done. Isn't it?
No.
One more question related: Why I cannot use the method run as following: Indeed.Inner.run();
Becuase it's not static.
static class is only valid for inner classes and you can point to a static class by its enclosing class as Indeed.Inner.
This is different from non-static inner class where you need an instance of the enclosing class to create an instance of the same class. For example:
Indeed.Inner inner = new Indeed().new Inner();
No, run() is an instance method of the static class Inner. A static (inner) class just makes it possible to use an instance of the class without an enclosing parent instance. When you do Indeed.Inner inner = new Indeed.Inner();, you are creating an instance of the static class, and you are invoking it's run() method on this instance.
A static class is just a regular class, in fact more so than a non-static class.
The difference between a static nested class and a top-level class is just access scoping: the static class can access private members of its enclosing class.
Once you get that cleared up, you won't need to ask the question that you are asking here.
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Non-static nested classes (inner classes) have access to other members of the enclosing class
A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class.
Static inner class
public static class Inner implements Runnable
means you can create the instance of them, without having to create the instance of outer class (Indeed)
Indeed.Inner inner = new Indeed.Inner();
Why I cannot use the method run as following: Indeed.Inner.run() ?
the run method is by default not static. To call Indeed.Inner.run() directly, you need to make run() method static too
I have started learning Java language for Android Application developement.
As per my understanding based on static class, we cannot instantiate object of static class.
But why instantiation of static nested class object is allowed in following situaltion?
class EnclosingClass
{
//...
class static StaticInnerClass
{
//...
}
}
Why we can create object of inner class if it is marked as static?
EnclosingClass.StaticInnerClass s = new EnclosingClass.StaticInnerClass()
As per my understanding based on static class, we cannot instantiate object of static class.
Your understanding of the meaning of "static class" is incorrect. Basically a "static class" in Java is a nested class which doesn't have an implicit reference to an instance of the containing class. See section 8.5.1 of the JLS for more information, in particular:
The static keyword may modify the declaration of a member type C within the body of a non-inner class or interface T. Its effect is to declare that C is not an inner class. Just as a static method of T has no current instance of T in its body, C also has no current instance of T, nor does it have any lexically enclosing instances.
Perhaps you were thinking of static classes in C#, which are completely different?
Why we can create object of inner class if it is marked as static?
You may need to use a nested class in a static context, for example:
public class Test {
public static void main(String args[]) {
InnerClass innerClass = new InnerClass();
}
class InnerClass {
}
}
In this case, when you try to instantiate the innerClass you get the error:
No enclosing instance of type Test is accessible. Must qualify the
allocation with an enclosing instance of type Test (e.g. x.new A()
where x is an instance of Test).
To avoid this, you could instantiate an object of type Test and create an instance of innerClass from it:
Test test = new Test();
InnerClass innerClass = test.new InnerClass();
or better, declare also the innerClass as static and instantiate it in a static context:
public class Test {
public static void main(String args[]) {
InnerClass innerClass = new InnerClass();
}
static class InnerClass {
}
}
check it, maybe it can help you
Nested Classes
public static class ViewHolder {
public int a,b;
public void method();
}
are the method and the variables a and b by default static when I declare the class as static or do I have to separately declare them static ? I know its a noobish question but I am a little confused right now :(
No, they're not static by default, they're normal instance members.
Static inner classes, unlike normal inner classes, can have static members, though, if you explicitly declare them.
No, when you declare an inner static class you specify that the declaration itself is static, so that you don't need an object instance of the parent class to access it.
Nothing regarding inner members is involed.
The members of the Static nested class are not static. static keyword is specified with the class which signifies that the nested class can be instantiated with the containing outer class similar to static data member.
BaseClass.StaticNestedClass nestedClass = new BaseClass.StaticNestedClass();
nestedClass.nonStaticMethod();//correct
BaseClass.StaticNestedClass.nonStaticMethod()//Error
This has no effect on the data members of the static nested class which behave as normal class.
Please note if a static keyword is associated with a class then the class has to be a nested class
A public static class works just like any other class. The only real difference is that it is accessed through the containing class:
OuterClass.InnerClass foo = new OuterClass.InnerClass();