abstract class Person {
abstract void eat();
}
class TestAnonymousInner {
public static void main(String args[]){
Person p=new Person() {
void eat(){System.out.println("nice fruits");}
};
p.eat();
}
}
Internal class generated by the compiler
static class TestAnonymousInner$1 extends Person
{
TestAnonymousInner$1(){}
void eat()
{
System.out.println("nice fruits");
}
}
For which reason does the compiler create the anonymous class as static? What would happen if it's non-static?
The code as shown creates an anonymous class in a static context. An inner class (or non-static nested class) needs to have a reference to the enclosing object(*). In this case there is no enclosing object because it is created in a static method, so using a static nested class is the only valid option.
This can easily be demonstrated by transforming your example to
public class TestInner {
public static void main(String args[]){
Person p = new Testperson();
p.eat();
}
public class Testperson extends Person {
void eat() {
System.out.println("nice fruits");
}
}
}
Compiling this will yield the error
non-static variable this cannot be referenced from a static context
While it will compile just fine if you change it to:
public class TestInner {
public static void main(String args[]){
Person p = new Testperson();
p.eat();
}
public static class Testperson extends Person {
void eat() {
System.out.println("nice fruits");
}
}
}
*: The compiler will modify the constructor(s) of an inner class to accept the enclosing object as a parameter, and constructor calls will be rewritten to pass this as the value of that parameter. This is not the case for static nested classes.
Related
So i am watching tutorials this is the code
abstract class AnonymousInner {
public abstract void mymethod();
}
public class OuterClass {
public static void main(String args[]) {
AnonymousInner inner = new AnonymousInner() {
public void mymethod( ) {
System.out.println("this is an example of anonymous inner class");
}
};
inner.mymethod();
}
}
but after i type in this is not working cause the OuterClass "the public type must be defined in its own file"
Don't know why and asking for help.
What's the name of the file? It should be OuterClass.java.
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.
I have a code-
public class Hello
{
void create()
{
Inner obj=new Inner();
obj.r=100; //Able to access private variable x
obj.display(); //displays 100
}
class Inner
{
private int r=45;
void display()
{
System.out.println("r is : "+r);
}
}
public static void main(String[] args)
{
Hello ob=new Hello();
ob.create();
}
}
In the above code,by creating an instance of the inner class,we are able to access the private variable defined in that class.But this is not in the case of inheritance.Why it is so?For e.g.,in this code-
class One
{
private int x;
void getData()
{
x=10;
}
void display()
{
System.out.println("x is : "+x);
}
}
class Two extends One
{
int y;
void putData()
{
One o=new One();
o.x=13; //Error
}
}
public class File
{
public static void main(String[] args)
{
Two to=new Two();
to.putData();
}
}
What is the exact reason behind it?Thanks in advance...
See the Java Language Specification.
Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (ยง7.6) that encloses the declaration of the member or constructor.
Meaning that a top-level class can access the private members of it's nested classes.
Or said another way: Private means private to the top-level class and all it's nested classes, not private to the nested class itself.
Precondition:A static method in the super class was invoked by a non static method in the same class, and that non static method was invoked by a object of one sub class.
My question is:
1.Compile-time: The subClassObject(now it is treated as SuperClass type) was bound to staticMethod()?
2.Run-time:The subClassObject(now it is treated as a object of subClass.) was bound to nonStaticMethod()?
package a;
public class SuperClass {
public static void staticMethod() {
System.out.println("Superclass.");
}
public void nonStaticMethod() {
staticMethod();
}
}
package a;
public class SubClass extends SuperClass {
public static void staticMethod() {
System.out.println("Subclass.");
}
}
package a;
public class Demo {
public static void main(String[] args) {
SubClass subClassObject = new SubClass();
subClassObject.nonStaticMethod(); // output: Superclass;
}
}
Thank you.
Static methods are bound to the class - they can not be overridden. The non static method calls the static method of its class, implicitly specifying SuperClass.staticMethod().
I have the following code with a nested class called out1
class sample{
public int a=5;
class out1{
void main1(){
System.out.println("this is out1");
}
}
void call(){
//access main1() method on class out1
}
}
public class innerclass{
public static void main(String args[]){
sample ob=new sample();
System.out.println(ob.a);// access field a on class sample
//access call() on class sample
}
}
does anyone know on how to access inner class out1 and is it possible to access this inner class without using call() method on class sample?
You can create inner class out1 object as
ob.new out1();
This is my way how to access inner classes. I made a get method in class sample which returns an object of class out1:
public class innerclass {
public static void main(String args[]) {
sample ob = new sample();
ob.getOut1().call(); // calling the call() method in innerclass out1
}
}
class sample {
public int a = 5;
out1 getOut1() {
return new out1();
}
class out1 {
public void main1() {
System.out.println("this is out1");
}
public void call() {
main1();
}
}
}
And try to make classes with uppercase letter and also use camelcase like: Sample, InnerClass, Out1.
You can access the innerclass by new of outer.new of inner.
To call inner class method from outer class you need to create an object of the inner class.Otherwise you have to make the inner class as well as the method static.
class Sample{
public int a=5;
class Out1{
void main1(){
System.out.println("this is out1");
}
}
void call(){
new Out1().main1();
}
}
public class Innerclass{
public static void main(String args[]){
Sample ob=new Sample();
System.out.println(ob.a);// access field a on class sample
Sample.Out1 out1=new Sample().new Out1();
Out1.main1();
ob.call();
//access call() on class sample
}
}
And class names should start with capital letter by convention.
Inner classes can be static.
If you do not define your inner class as static, then you have to create an instance of it (an object) in order to use it.
Here is an example use case of static and member (non static) classes:
public class Tester {
public static void main() {
Outer outerTest = new Outer();
outerTest.test();
outerTest.publicInnerInstance.sayHello();
Outer.InnerStaticClass.sayHello();
}
}
class Outer{
class InnerMemberClass{
public void sayHello(){
System.out.println("Hello");
System.out.println("I'm an instance of 'InnerMemberClass'.");
}
}
static class InnerStaticClass{
public static void sayHello(){
System.out.println("Hello.");
System.out.println("I'm a static class 'InnerStaticClass'.");
}
}
public InnerMemberClass publicInnerInstance;
//'Outer' constructor
public void Outer(){
publicInnerInstance = new InnerMemberClass();
}
public void test(){
InnerStaticClass.sayHello();
InnerMemberClass instance = new InnerMemberClass();
instance.sayHello();
}
}