This question already has answers here:
Why can't we have static method in a (non-static) inner class (pre-Java 16)?
(15 answers)
Closed 8 years ago.
Im trying to understand how inner class works and while experimenting with some simple code i got a error : The method hello cannot be declared static; static methods can only be declared in a static or top level type
on this code
public class Class1 {
public static void main(String[] args) {
Class1 c = new Class1();
c.show();
}
public static void show() {
class C2 {
static public void hello() {
System.out.println("show class");
}
}
C2.hello();
}
}
and i cant understand why!
Refer to the documentation here.
Inner Classes: As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.
Class2 is an inner class which means that it needs to be tied to an Class1 object. Then objects of Class2 can access the fields of the bound object at all times:
public class Class1 {
private String name = "class1";
public static void main(String[] args) {
Class1 a = new Class1();
Class2 c = a.new Class2();
c.show();
}
class Class2 {
public void show() {
System.out.println("helloworld: "+name); //accessing the name field of a without needing the variable
}
}
}
or you need to make Class2 static so it doesn't need the Class1 instance.
public class Class1 {
public static void main(String[] args) {
Class2 c = new Class2();
c.show();
}
static class Class2 {
public void show() {
System.out.println("helloworld");
}
}
}
Class C2 in your example above is a local Inner class, which means an inner class defined within a method of an outer class, and such classes cannot have static methods inside them because they are associated with objects, (static methods are not dependent upon objects).
Moreover, a local inner class must be instantiated within the method it has been created and not outside the method. This is a rule.
try modifying your code in following way:
public class Class1 {
public static void main(String[] args) {
Class1 c = new Class1();
c.show();
}
public static void show() {
class C2 {
public void hello() {
System.out.println("show class");
}
}
C2 obj= new C2();
obj.hello();
}
}
This should work.
You cant do this since you need to create an instance of Class 'Class1' before you can access Class 'C2'. However the method 'hello' should be possible to access without creating an instance (being a static method).
Related
This question already has answers here:
"Non-static variable this cannot be referenced from a static context" when creating an object
(5 answers)
Closed last year.
import java.util.*;
public class MyClass {
MyClass()
{
}
public static void main(String args[]) {
Node n = new Node();// CT error - non-static variable this cannot be referenced from a static context
MyClass obj = new MyClass();//works (Why? Since this is also a non-static)
test t = new test();//works (Why? Since this is also a non-static)
}
class Node{
};
}
class test{
}
How can main method (which is static) call it's own class' constructor even if it is non-static? And can't call nested class' constructor.
It's important to note the distinction between calling a static method, which you can do whenever, and calling a non-static method, which must be done on an instance.
class Foo {
public void run() {
System.out.println("Hello, world!");
}
public static void static_run() {
Foo f = new Foo();
f.run();
}
public static void main(String[] args) {
Foo.static_run(); // Calling a static method from a static context
(new Foo()).run(); // Calling a non-static method on an instance of Foo
Foo.run(); // Error! Cannot call a non-static method from a static context
A a = new Foo.A(); // This is fine since A is a static class
B b1 = new Foo.B(); // Error! B is non-static but is being called from a static context
B b2 = new (new Foo()).B(); // This is fine since B is created on an instance of Foo, not from a static context
C c = new C(); // This is fine since top-level classes are implicitly static (i.e. they must be static)
}
static class A {}
class B {}
}
class C {}
Notice how we can create instances of our class within it's own static methods.
class A { //1st code starts here
private void display() {
System.out.println("A class");
}
}
class B extends A {
protected void display() {
System.out.println("B class");
}
}
class Test {
public static void main(String args[]) {
A obj = new B();
obj.display();
}
}
Output : Test.java:22: error: display() has private access in A
obj.display();
class Outer{ //2nd Code starts here
class Inner1{
private void m2() {
System.out.println("Inner1 class");
}
}
class Inner2 extends Inner1{
protected void m2() {
System.out.println("Inner2 class");
}
}
public static void main(String args[]) {
Outer o=new Outer();
Outer.Inner1 i=o.new Inner2();
i.m2();
}
}
Output : Inner1 class
Why compile time error in 1st code while output Inner1 class in 2nd code???
The code of the Outer class can access any member or method declared within the Outer class, regardless of the access level. However, the m2 method being called is the method of the base class Inner1, since you can't override a private method.
On the other hand, the code of the Test class cannot access a private method of a different class, which is why that code doesn't pass compilation.
Because private members are accessible only in class.
when class B extends A private members are inaccessible For B.
In case of Inner classes, An inner class is a member of class and have access to all members of enclosing class.
Can a innerclass also be a subclass. Also one more thing in this set of java planguage it's not allowing me to create a instance of subclass even though I already created a instance of my encapsulating class for the innerclass.
public class Main {
Main OpTypes[] = new Main[3];
public static void main(String[] args) {
Main c = new Main();
c.OpTypes[0] = new Division(6,3);
Jool x = new Jool();
}
public class Jool {
public Jool() {
}
}
}
If you try it, you'll find that an inner class can extend a class.
Also, since the inner class is not static, it requires an instance of the outer class when constructing it. In the code below, you'll see two ways of doing that.
Method test shows the most common way, which is to do it from an instance (non-static) method of the outer class, in which case the outer class is implicit.
Method main shows how to do it outside of an instance method of the outer class, in which case you have to give an outer class instance before the new operator.
class MyBaseClass {
}
class Main {
public static void main(String[] args) {
Main c = new Main();
Jool x = c.new Jool(); // "c" explicitly used as outer class instance
}
public void test() {
Jool x = new Jool(); // "this" implicitly used as outer class instance
}
public class Jool extends MyBaseClass { // Inner class extends unrelated class
}
}
This question already has answers here:
Why can't we have static method in a (non-static) inner class (pre-Java 16)?
(15 answers)
Closed 8 years ago.
Im trying to understand how inner class works and while experimenting with some simple code i got a error : The method hello cannot be declared static; static methods can only be declared in a static or top level type
on this code
public class Class1 {
public static void main(String[] args) {
Class1 c = new Class1();
c.show();
}
public static void show() {
class C2 {
static public void hello() {
System.out.println("show class");
}
}
C2.hello();
}
}
and i cant understand why!
Refer to the documentation here.
Inner Classes: As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.
Class2 is an inner class which means that it needs to be tied to an Class1 object. Then objects of Class2 can access the fields of the bound object at all times:
public class Class1 {
private String name = "class1";
public static void main(String[] args) {
Class1 a = new Class1();
Class2 c = a.new Class2();
c.show();
}
class Class2 {
public void show() {
System.out.println("helloworld: "+name); //accessing the name field of a without needing the variable
}
}
}
or you need to make Class2 static so it doesn't need the Class1 instance.
public class Class1 {
public static void main(String[] args) {
Class2 c = new Class2();
c.show();
}
static class Class2 {
public void show() {
System.out.println("helloworld");
}
}
}
Class C2 in your example above is a local Inner class, which means an inner class defined within a method of an outer class, and such classes cannot have static methods inside them because they are associated with objects, (static methods are not dependent upon objects).
Moreover, a local inner class must be instantiated within the method it has been created and not outside the method. This is a rule.
try modifying your code in following way:
public class Class1 {
public static void main(String[] args) {
Class1 c = new Class1();
c.show();
}
public static void show() {
class C2 {
public void hello() {
System.out.println("show class");
}
}
C2 obj= new C2();
obj.hello();
}
}
This should work.
You cant do this since you need to create an instance of Class 'Class1' before you can access Class 'C2'. However the method 'hello' should be possible to access without creating an instance (being a static method).
For example:
public class A {
public class B extends A {
}
public static void main(String[] args) {
A a;
a = new B();
}
}
I searched some similar questions and they showed "yes, a superclass type variable can refer to a subclass object". But in eclipse the above code comes up with an error like "No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A (e.g. x.new A() where x is an instance of A)."
So what wrong? Thanks!
The answer is "yes" superclass can refer to a subclass, but you're asking the wrong question.
You're getting this error because B is an enclosed class of A (meaning you must have an instance of A to have an instance of B), but you're referring to it from a static method (ie not an instance of A).
Simply change B to be a static class.
public class A {
public static class B extends A { // <-- Added static keyword
}
public static void main(String[] args) {
A a;
a = new B();
}
}
No errors.
The other option is leave it an enclosed class and do this:
public class A {
public class B extends A { // leave B as an enclosed class
}
public static void main(String[] args) {
A a = new A();
a = a.new B(); // can only create a B in the context of an A
}
}
check your parantheses!! It should be:
class A {
}
public class B extends A {
public static void main(String[] args) {
A a;
a = new B();
}
}
Also you have two public classes in your code!!