Access field of outer base class - java

In Java, inner classes can normally access outer classes' private members.
While writing an Android app, I had a static inner class which extended it's outer class.
Turns out, private fields of the outer class could not be accessed:
class Outer {
private int m_field = 1;
static class Inner extends Outer {
Inner() {
m_field = 2;
}
}
}
It gives a confusing error message:
error: non-static variable m_field cannot be referenced from a static context
Even though nothing is static except the class itself.
When the field m_field is made protected, it compiles without a problem.
But also, when doing this:
class Outer {
private int m_field = 1;
static class Inner extends Outer {
Inner() {
((Outer)this).m_field = 2;
}
}
}
It works without problems.
Is this a bug in the compiler? Why would you need to cast to the outer class, which you are already an instance of?
EDIT:
For a real use-case of this, consider a class like this:
public abstract class MyItem {
private int m_counter = 0;
public abstract int updateSomething();
public static class CountItem extends MyItem {
public int updateSomething() { m_counter++; }
}
public static class DoubleCountItem extends MyItem {
public int updateSomething() { m_counter += 2; }
}
}
Pretty abstract example, but it can be used to provide basic implementations for abstract classes which don't need a lot of code on their own.
EDIT2:
As #Nathan suggests, it seems like this problem can be recreated by 2 classes without nesting:
class Base {
private int x = 0;
void a(Extended b) {
((Base)b).x = 1; //<-- with cast: compiles, without: error
}
}
class Extended extends Base {
}
Which gives better error message:
error: x has private access in Base

What you are seeing here is that, as long as you're within the class definition of Outer, you have access to private members of anything that has the class Outer, including things that are cast to Outer. They have to have the same class (as opposed to being instances of the class, with a different concrete subclass).
The inner class thing is a complication, here's a smaller example:
public class A {
private int foo = 0;
public String toString() {
return "A: foo=" + foo;
}
public static void main(String[] args) {
B b = new B();
System.out.println(b);
((A)b).foo = 1;
System.out.println(b);
}
}
class B extends A {
}
This compiles, only because it's within the class definition of A. Move the main method somewhere else (to within B, for instance), and you can no longer reference foo.
This is something that you see when coding equals methods, where you can access private fields of another instance of the same class because you're writing a method that's part of the class definition.
The Java language specification, in 6.6.1 Determining Accessibility:
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.
Without the cast to Outer access is not permitted because a) m_field is a private member of Outer, so it's not visible to subclasses, and b) it's not a member of the class being declared. Adding the cast means the compiler treats it as an Outer, and m_field becomes accessible.

Related

How to get attributes of a parent class in Java?

How do I get the attributes of Class A, a parent class (super class), to use it in Class C in Java.
For instance:
Class B extends A
Class C extends B
You need to declare the member protected:
public class A
{
protected int myInt = 5;
}
public class B extends A
{
}
public class C extends B
{
public int GetInt()
{
return myInt;
}
}
private member can be accessed only by the class itself, protected by the class and all the derived classes.
Typically it is best to keep attributes private, and access them via accessor (getter) and mutator (setter) methods from any other class, including derived classes. If the variable must or should be accessed directly from subclasses, which occasionally is desirable but not usually, then nearly always declare it protected.

Why can enum implementations not access private fields in the enum class

I just answered this question by saying how to solve the compilation problem:
How to use fields in java enum by overriding the method?
But what I don't understand is why the error is happening in the first place.
Here is the example written as an enum:
public enum MyEnum {
FIRST {
#Override
public String doIt() {
return "1: " + someField; //error
}
},
SECOND {
#Override
public String doIt() {
return "2: " + super.someField; //no error
}
};
private String someField;
public abstract String doIt();
}
Here is the exact same thing as abstract classes
abstract class MyClass {
class FIRST extends MyClass {
#Override
public String doIt() {
return "1: " + someField; //no error
}
};
class SECOND extends MyClass {
#Override
public String doIt() {
return "2: " + super.someField; //no error
}
};
private String someField;
public abstract String doIt();
}
In the case of FIRST within the enum implementation it cannot access someField. However in the abstract class case it can.
Additionally adding super fixes the problem, as does removing the private modifier on the field.
Does anyone know why this slight quirk in the behaviour is happening?
Your abstract class is not equivalent to your enum, since enums are implicitly public static final. Thus, you'll observe the same behavior if you use:
abstract class MyClass {
static class FIRST extends MyClass {
#Override
public String doIt() {
return "1: " + someField; // error
}
};
static class SECOND extends MyClass {
#Override
public String doIt() {
return "2: " + super.someField; // no error
}
};
private String someField;
public abstract String doIt();
}
As explained in http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html, chapter "Static Nested Classes":
A static nested class cannot refer directly to instance variables or
methods defined in its enclosing class: it can use them only through
an object reference.
Thus the need of super. You could also use this if the field were protected rather than private.
When an identifier is resolved, Java prefers the lexical scope over inherited members. So when you have an inner class that extends the outer class and use a field of the outer class without using this or super, the field of the outer instance is accessed which fails if the inner class is static as there is no outer instance then. In contrast, when using super you are explicitly accessing the inherited member. Note that enum classes are implicitly static. You can even use this to access the inherited member but you have to use ((MyClass)this).someField to access it if it’s declared private.
Class FIRST is an inner class of MyClass and also a sub class. The reason you do not see an error when accessing someField in it is because you are accessing the someField of the outer class, not the super class.
class MyClass {
class FIRST extends MyClass {
#Override
public String doIt() {
super.someField = "super";
return "1: " + someField;
}
};
private String someField = "outer";
public String doIt(){return "";}
public static void main(String[] args) {
System.out.println(new MyClass().new FIRST().doIt());
}
}
Prints 1: outer.
In the other case your enum constants behave as static nested sub classes, not inner classes, so they do not have a reference to the outer class, only their super class.
I disagree with the accepted answer.
The enum const declaration is implicit public static final, but not the class the enum const belongs to.
From JSL Chapter 8.Classes
The optional class body of an enum constant implicitly defines an anonymous class declaration (§15.9.5) that extends the immediately enclosing enum type. The class body is governed by the usual rules of anonymous classes.
And what the 'rules of anonmous classes'?
From JSL Chapter 15:
An anonymous class declaration is automatically derived from a class instance creation expression by the Java compiler.
An anonymous class is never abstract (§8.1.1.1).
An anonymous class is always implicitly final (§8.1.1.2).
An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1).
And if the enum equivalent class is a static class, how to explain the following error?
public enum MyClass {
First {
public static int b; //(2)Illegal static declaration in inner class
};
}
But why a inner class can't access the outer class's field?
A possible enum equivalent class may looks like following, which gives the same error as a enum class:
abstract class MyClass {
private int someField;
static {
class First extends MyClass {
public void method() {
System.out.println(someField);
}
private static int b;
}
}
}
More:
Nested enum is static?
How is enum implemented?

Compiler errors when static abstract Inner class accesses outer class private fields java

I have a class with private fieds and also a static abstract inner class with the generic type extending the outer class type, that tries to access the outer class private fields but get's the following error:
- error: a has private access in Outer
- error: doSomething has private access in Outer
See code below:
public abstract class Outer extends SomeOuter
{
private Object a;
private void doSomething(){}
public static absract class Inner<T extends Outer> extends SomeOuter.SomeInner<T> {
public InnerMethod(T p) {
p.a; //error: a has private access in Outer
p.doSomething() //error: doSomething has private access in Outer
}
}
}
I'm compiling using jdk 1.7
Can anyone please tell me why am getting this error.
An Inner class can access private field of enclosing class and a static inner class can also access any private members of enclosing class .
The class itself isn't really "static"; there's no such thing as a static class. The
static modifier in this case says that the nested class is a static member of the outer
class. That means it can be accessed, as with other static members, without having
an instance of the outer class.
Since it is a static member , it is able to access outer class's private members , because within class private members are accessible.
eg.
class One
{
private int i=0;
class Two
{
void go()
{
System.out.println(new One().i); //accessible
}
}
}
class two
{
private int i=3;
static class one
{
void go()
{
System.out.println(new two().i); //accessible in static class
}
}
}
But here ,
Inner<T extends Outer> extends SomeOuter.SomeInner<T>
T is a class which extends Outer , doesn't mean it is inner.
That why it is giving error.
thats how private modifier works if you ever declare any method or variable as private than that things can not be accessed out side the class
A static embedded class is effectively an outer class. It can't access the private members of another class. See the accepted answer to:
Static nested class in Java, why?
Both the Object and the function you're trying to use are declared as private, which means they cannot be used outside the Object. If you want to use them in child classes as well, declare them as protected.
Changes the scope of the fields on the Outer class to protected so that classes extending Outer may access these fields.
public abstract class Outer extends SomeOuter
{
protected Object a;
protected void doSomething(){}
public static absract class Inner<T extends Outer> extends SomeOuter.SomeInner<T> {
public InnerMethod(T p) {
p.a; //error: a has private access in Outer
p.doSomething() //error: doSomething has private access in Outer
}
}
}
Thats what you have private modifier for. Though it is the inner class, it cannot access the private members of the outer class. So, declare it as protected, as you are extending the outer class to inner class

java private access modifier can be accessed out side of the class?

Why java compiler is not restricting from accessing a private attribute from other class?
I have inner class which has a attribute 'a' with modifier 'private'. I can able to access this variable with its instance variable out side the class. see the code below.
package com.test;
public class Test {
public Test() {
}
public static void main(String[] args) {
new Test().execute(); // test method
}
public void execute() {
InnerClass innerClassInstance = new InnerClass();
// accessing private member from other class instance, HOW?
System.out.println(innerClassInstance.a);
InnerStaticClass innerStaticClassInstance = new InnerStaticClass();
// accessing private member from other class instance, HOW?
System.out.println(innerStaticClassInstance.a);
}
private final class InnerClass {
private int a; // accessible only in InnerClass??
}
private final static class InnerStaticClass {
private int a; // accessible only in InnerClass??
}
}
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Sorry, I misread the question.
Looks at JLS http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.1
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.
So, a field in inner-class (which is obviously inside class body) can be accessed by outer class even if it's private.
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.
your Inner class is a member of your Test class so, Test class can access private member of Inner class too.
Try like this it will give compile error
public class Test {
public Test() {
}
public static void main(String[] args) {
new Test().execute(); // test method
}
public void execute() {
InnerClass innerClassInstance = new InnerClass();
// accessing private member from other class instance, HOW?
System.out.println(innerClassInstance.a);
InnerStaticClass innerStaticClassInstance = new InnerStaticClass();
// accessing private member from other class instance, HOW?
System.out.println(innerStaticClassInstance.a);
}
}
class InnerClass {
private int a; // accessible only in InnerClass??
}
final class InnerStaticClass {
private int a; // accessible only in InnerClass??
}
These nested classes are meant for that .When you have private
members in a class ,we write nested class to do unit testing.

Can a Static Nested Class be Instantiated Multiple Times?

Given what I know of every other type of static feature of programming––I would think the answer is 'no'. However, seeing statements like OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); makes me wonder.
Yes, there is nothing in the semantics of a static nested type that would stop you from doing that. This snippet runs fine.
public class MultipleNested {
static class Nested {
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Nested();
}
}
}
See also
public static interface Map.Entry<K,V>
public static class AbstractMap.SimpleEntry<K,V>
Probably the most well-known nested type. Obviously instantiated multiple times.
Now, of course the nested type can do its own instance control (e.g. private constructors, singleton pattern, etc) but that has nothing to do with the fact that it's a nested type. Also, if the nested type is a static enum, of course you can't instantiate it at all.
But in general, yes, a static nested type can be instantiated multiple times.
Note that technically, a static nested type is not an "inner" type.
JLS 8.1.3 Inner Classes and Enclosing Instances
An inner class is a nested class that is not explicitly or implicitly declared static.
That is, according to JLS terminology, an inner class is one that isn't static. If it's static, then it's just a nested type.
So what does static mean?
static simply means that the nested type does not need an instance of the enclosing type to be instantiated.
See also
Java inner class and static nested class
Java: Static vs non static inner class
#polygenelubricants :
But in general, yes, a static nested
type can be instantiated multiple
times.
Just to be sure 100% of that I extended your snippet:
public class MultipleInner {
static class Inner {
private int state;
public int getState() { return state; }
public void setState(int state) { this.state = state; }
}
public static void main(String[] args) {
List<Inner> inners = new ArrayList<Inner>();
for (int i = 0; i < 100; i++) {
Inner inner = new Inner();
inner.setState(i);
inners.add(inner);
}
for (Inner inner : inners) {
System.out.println(inner.getState());
}
}
}
And of course the result is:
0
1
2
3
.
.
.
97
98
99
It is legal. The fact that the inner class is static gives you a benefit here; its instances are not bound to any instance of the containing class, so they can be freely instantiated (as long as the access qualifier allows it).
The price, however, is that the inner class can't use non static members/methods of the containing class.
Yeah you can make instances of it as many times as you want.
Maybe the reason why you see that, is because the programme thought about storing a reference somewhere. Though i agree with you seems strange :S
Inner class can use non static members/methods of containing class. It can use them only through an object reference of the enclosing class-
public class MultipleInner {
private int outerstate =10;
static class Inner {
private int state;
public int getState() { return state; }
public void setState(int state) { this.state = state; }
}
public static void main(String[] args) {
Inner inner = new Inner();
inner.setState(new MultipleInner().outerstate);
System.out.println(inner.getState());
}
}
So, inner class doesn't have to pay the price of not being able to access the non static members of the enclosing class.
Static nested classes are indeed instanced - they are, as said, top-level classes which live in the namespace of the 'outer' class, and obey static semantics respecting references to the 'outer' class. This code sample demonstrates :
public class OuterClass {
String outerStr = "this is the outer class!!" ;
public static class StaticNestedClass {
String innerStr = "default / first instance" ;
}
public static void main(String[] args) {
OuterClass.StaticNestedClass nestedObject1 = new OuterClass.StaticNestedClass();
OuterClass.StaticNestedClass nestedObject2 = new OuterClass.StaticNestedClass();
nestedObject2.innerStr = "second instance" ;
System.out.println(nestedObject1.innerStr) ;
System.out.println(nestedObject2.innerStr) ;
}
}
output:
default / first instance
second instance

Categories