Why can't we declare private local inner class inside a method? - java

class outer
{
public void outerfunction()
{
private class inner // Line-1
{
public void showinner()
{
System.out.println("I am in Inner Class");
}
}
}
}
Line-1 : This Line gives error.
I know if I write abstract or Final for class inner. then,it is fine. Why class inner can't be private ?

A private class is visible to all the methods and nested classes in the same file. Normally you would think of private as narrowing the scope of use, but in this case using private you are broadening the scope of where is the class is visible in a way Java doesn't support.
Making the method private is the same as doing this
class outer {
private class inner { // Line-1
public void showinner() {
System.out.println("I am in Inner Class");
}
}
public void outerfunction() {
}
}
Which looks fine until you consider that a nested class can use the values in it's outer context.
class outer {
public void printInner() {
// accessible in the same .java file because we made it private
inner i = new inner();
i.showInner(); // what should `x` be?
}
public void outerfunction(final int x) {
private class inner { // Line-1
public void showinner() {
System.out.println("I am in Inner Class, x= " + x);
}
}
}
}

Related

Why can't I access local class outside declaring method?

I recently bumped into a rare -- but perfectly legal syntax: Local Classes.
I was wondering why can't I access a local class outside that method? With what is it different from an inner class which can be access in the outer class or with an enclosed object (outer.new Inner())?
Example: this is perfectly valid,
//this is valid
public class Outer {
int outer_x = 100;
public void test() {
Inner inner = new Inner();
inner.display();
}
public class Inner {
public void display() {
System.out.println("Outer x is: " + outer_x);
}
}
}
This is valid as well
//this is valid as well
public class Outer {
int outer_x = 100;
public void test() {
Inner inner = new Inner();
inner.display();
}
public class Inner {
public void display() {
System.out.println("Outer x is: " + outer_x);
}
}
public void test2() {
Inner inner2 = new Inner();
inner2.display();
}
}
But this will not compile:
public class Outer {
int outer_x = 100;
public void test() {
class Inner {
public void display() {
System.out.println("Outer x is: " + outer_x);
}
}
Inner inner = new Inner();
inner.display();
}
public void test2() {
Inner inner2 = new Inner(); // error here
inner2.display();
}
}
Why is this so?
Because every call to test() creates a complete new version of the class. Therefore it may access (final or effectively final) local variables!
Here's a rundown what's going on.
public class Outer {
public void test(int i) {
class Inner {
private int x = i; // i may be different on each call of test
public void display() {
System.out.println("Inner x is: " + x);
}
}
Inner inner = new Inner();
inner.display();
}
public void test2() {
test(1); // prints 1
test(2); // prints 2
//now imagine this is valid
Inner inner2 = new Inner();
inner2.display();// what's the value of x??
}
}

How do I create an instance of a inner class in the outer class constructor

I switched from C++ to Java and have a problem with nested classes. I would like to initiate an instance of a subclass in the constructor of the superclass. I tried it like this, but it seems to be wrong.
public class Aircraft {
public class LandingGear {
}
public Aircraft() {
Aircraft.LandingGear myLandingGear = this.new LandingGear();
}
}
The idea is that every instance of the class Aircraft has an instance "myLandingGear" of the subclass LandingGear.
For this use case, your nested class should be static (if it's going to be nested at all), and then you just use a simple new:
public class Aircraft {
public static class LandingGear {
// ----^
}
public Aircraft() {
Aircraft.LandingGear myLandingGear = new LandingGear();
// --------------------------------------^^^^
}
}
When it's not static, it's an inner class, which from your description isn't what you want. More on nested classes in this Java tutorial.
But unless there's a really good reason for LandingGear to be nested inside Aircraft, consider making it a peer instead.
Every Aircraft will have a LandingGear
public class Aircraft {
private LandingGear myLandingGear;
public Aircraft() {
myLandingGear = new LandingGear();
}
public LandingGear getLandingGear() {
return this.myLandingGear;
}
}
public class Aircraft {
public class LandingGear {
public static void m1()
{
//Your code snipet
}
}
public Aircraft() {
LandingGear.m1();
}
}

Visibility of inner class variables

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.

How to access a method of Extended Class from nested class?

I have two nested classes inside a class with the outer class extending another class. The structure is something like this.
public class EXTENSION_CLASS
{
public int Get_Value()
{
return(100);
}
}
public class OUTER extends EXTENSION_CLASS
{
public static class NESTED1
{
public void Method1()
{
int value=0;
value=Get_Value();
System.out.println("Method1: "+value);
}
}
public static class NESTED2
{
NESTED1 Nested1_Instance=new NESTED1();
public void Method2()
{
Nested1_Instance.Method1();
}
}
public void run()
{
NESTED2 Nested2_Instance=new NESTED2();
Nested2_Instance.Method2();
}
public static void main (String[] args)
{
OUTER New_Class=new OUTER();
New_Class.run();
}
}
I'm expecting the output: "Method1: 100". But, am getting the output: "OUTER.java:16: error: non-static method Get_Value() cannot be referenced from a static context value=Get_Value();". How can i make this working?
Cheers !
Rajesh.
Remove the static modifier from the declaration of NESTED1 and NESTED2, like so:
public class NESTED1
If you want keep the nested classes static, you will have to create an instance of OUTER in the Method1() to access Get_Value().
public void Method1()
{
int value=0;
OUTER outer = new OUTER();
value=outer.Get_Value();
System.out.println("Method1: "+value);
}
You're trying to make a static reference to a non-static member.
This means that you're trying to access a instance member from a static member of the class.
To fix the issue, remove the static modifier from both the NESTED1 and NESTED2 clases.
Alternately, if you do not wish to remove the static modifier, you will have to create an object of the OUTER or EXTENSION_CLASS classes and then invoke Get_Value() using the object.
For example:
public void Method1()
{
int value=0;
EXTENSION_CLASS ext = new EXTENSION_CLASS ();
value=ext.Get_Value();
System.out.println("Method1: "+value);
}
OR
public void Method1()
{
int value=0;
OUTER outer = new OUTER();
value=outer.Get_Value();
System.out.println("Method1: "+value);
}
The error says every thing. You can not call non-static methods from static methods / class.
To make it work, Simply add static keyword to 'Get_Value' method in EXTENSION_CLASS.

How to access a public method in a private static inner class from another class?

How to access the public function of private static inner class in some other class Suppose there is a class structure like below :-
public class Outer{
private static Inner {
public void func() {
}
}
}
And there is another class :-
class UseFunc {
// I have to use the func() here
}
If I use like this it will give error : - create object of Inner like Outer.Inner oi = new Outer.Inner();
access oi.func() //as Inner is private class
Okay, this is a very bad (I mean, really terrible) implementation but it works:
public class Outer
{
private static class Inner
{
public void func()
{
}
}
public void encapsulatedFunc()
{
new Inner().func();
}
}
class UseFunc
{
public static void main(String[] args)
{
new Outer().encapsulatedFunc();
}
}
I can only imagine that code being used for educational purpose as a "What not to do" example.

Categories