This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
protected/public Inner Classes
I am sure the question has been asked already, but I wasn't able to find one so I'll ask...
I am curious what is the difference between private(protected) and public inner class. I am able to use both from out of the containing class using the outer class object.
public class A{
private class B{
}
public static void main(String[] args){
A a = new A();
B b = a.new B();
}
}
A private inner class can still be accessed within the class that defined it.
If you have another class, B isn't visible:
public class C {
public static void main(String[] args){
A a = new A();
B b = new B(); // compile error
}
}
Actually, you are inside class A still, since the main method is a static method of class A
Related
This question already has answers here:
Are static variables inherited
(4 answers)
Closed 4 years ago.
I have a code in which I expect the output to be different from the actual output.. As static variables are reference based, I expect the output to be "superclass" but what I am getting is "subclass".. Code:
class TestClass {
public static void main(String args[] ) throws Exception {
A b = new B(); // Since the reference is A, "superclass" should be the output
b.test();
}
}
abstract class A{
static String a = "superclass";
abstract void test();
}
class B extends A{
static String a = "subclass";
void test(){
System.out.println(a); // Output subclass
}
}
Please tell me where am I wrong..
Static variables are not inherited in java. You varibale static String a is static which associates it to a class. Java inheritance doesn't work with static variables.
If you absolutely want the superclass variable you could use:
System.out.println(super.a);
Here is the inheritance what you probably wish to see:
abstract class A {
String a = "superclass";
abstract void test();
}
class B extends A {
void test() {
System.out.println(a); // Output superclass
}
}
I remove the static identifier and removed the subclass's implementation of variable a. If you run this you'll get superclass as output.
A b = new B();
First off, static variables are not inherited in Java. This means that when you create your object as a new B(), even though that class extends class A, it won't keep the definition of your String a.
static String a = "subclass";
Secondly, even if that were the case, you're immediately overriding the value of String a at the beginning of this class B. You specifically set it to be "subclass" just before printing its value, so of course you've overriden the original value with this new one.
Finally, it would be a wonderful idea to try and name things with a little more variety. There being a class A and a String a doesn't help much for readability, for you or the people answering your question.
public abstract class A
{ static int a = 1; }
public class B extends A
{ static int a = 2; public A() { } }
public static void main(String argv[])
{
A var1 = new B();
System.out.println(var1.a)
// Re-cast the variable to type "class B"
// This is the SAME VARIABLE
// It is occupying the SAME MEMORY SPACE
// This println will produce the same output...
System.out.println(((B) var1).a)
}
This question already has answers here:
Why doesn't Java allow overriding of static methods?
(22 answers)
Is it possible to override a static method in derived class?
(6 answers)
Closed 5 years ago.
class B extends A {
static public void printMe(){
System.out.println("in static B");
}
}
class A{
static public void printMe(){
System.out.println("In static A");
}
}
public static void main(String[] args) {
A a = new B();
a.printMe();
}
Why is the output "In static A" ?
Static members bind to type rather than implemented type. Hence you see the methods executing from class A.
And static members are not to be ovverriden and they share same copy regardless of instance state.
If you need methods to be ovveriden, do not use them as static members.
Static member or method belongs to class level instead of specific instance.
A static class will have only 1 instance even if you create multiple instance or do not create instance.
In your case, since you have created instance of class A, method inside class A is implemented.
Another way to get a clear concise for the problem scenario is try running the code mentioned below:
public static void main(String[] args) {
A.printMe();
}
You will get clear idea.
This question already has answers here:
Protected member access from different packages in java - a curiosity [duplicate]
(9 answers)
Closed 7 years ago.
I am very confused with this . can any one clear me why it was not allowing the code that i have placed n comments
package pack1;
public class A {
protected void m1() {
System.out.println("This is very imp point");
}
package pack2;
import pack1.A;
class B extends A {
public static void main(String arg[]) {
// A a1 = new A();
//a1.m1();
B b1 = new B();
b1.m1();
//A a2 = new B();
//a2.m1(); }
}
}
Method m1 is protected, so it's accessible across packages to child classes.
Therefore, instances of B will be able to invoke or #Override m1.
Not so main static methods, even if pertaining to class B: the scope is different.
You can either make m1 public in A, or invoke it within your instance of B (e.g. in the constructor, etc.).
You can also override A's m1 in B and give it less access restrictions, thus making it public in this instance: then you could access it on an instance of B from your main method as you're trying to do.
You can access the protected members declared in A within B, but only for instances of B or subclasses of B. Check out this answer
This question already has answers here:
What causes error "No enclosing instance of type Foo is accessible" and how do I fix it?
(11 answers)
Closed 8 years ago.
I am having a trouble using nested classes in Java, does anyone know why Java does not allow me to do it?
public class A{
private class B{
public B(){
System.out.println("class B");
}
}
public static void main(String[] args){
A a = new A();
B b = new B();
}
}
Because you are trying to access a non-static inner-class from a static method.
The 1st solution will be to change your inner-class B to static:
public class A{
private static class B {
public B() {
System.out.println("class B");
}
}
public static void main(String[] args){
A a = new A();
B b = new B();
}
}
a static inner-class is accessible from anywhere, but a non-static one, requires an instance of your container class.
Another solution will be:
A a = new A();
B b = a.new B();
This will give you a better understanding of how inner classes work in Java: http://www.javaworld.com/article/2077411/core-java/inner-classes.html
A a = new A();
B b = a.new B();
can solve your problem
You used private inner class.How can you get instance outside the A class?
public class JustForShow {
public class JustTry{
public JustTry() {
System.out.println("Initialized");
}
}
public static void main(String[] args) {
JustForShow jfs = new JustForShow();
JustTry jt = jfs.new JustTry();
}
}
You are trying to access a non-static membor from a static method. To solve that, you have two options:
Change your class B to be static, so add the static keyword in the class definition, like this:
public static class B { // ...
Change your main method, and use the created instance a to create B, like this:
B b = a.new B();
If B doesn't use any non-static resources of class A, I would recommend to use the first method.
public static void main(String[] args){
A a = new A();
A.B b = a.new B();
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a question about this topic:
An instance by a inner class need everytime an object of the same type of the guest class of the inner class. example:
public class GuestClass {
class innerClass {
}
public static void main(String[] args) {
innerClass test = new innerClass(); //does not work because
//I need a object of type GuestClass
GuestClass iNeedYou = new GuestClass();
innerClass nestClassObj = iNeedYou.new innerClass(); //it works
}
}
ok it's clear.
the innerClass object now points also to an GuestClass object (iNeedYou)
Now come my question:
an anonymous class is also a inner class right?
but with some difference:
A. I don't know the type of this object
B. it implements an interface.
but it is still an object by an inner class (anonymus but inner)
in fact if I do this:
public class GuestClass {
private int numb = 100;
class innerClass {
}
public void createAnAnonymusObject () {
myInterface myAnObj = new myInterface(){
int a, b;
#Override
public void print() {
System.out.println(numb); //it works
}};
myAnObj.print();
}
public static void main(String[] args) {
GuestClass iNeedYou = new GuestClass();
iNeedYou.createAnAnonymusObject();
}
}
works because the anonymous inner object points to the outer object... so i can see the variable numb.
but why this works?
public static void main(String[] args) {
myInterface myAnObj = new myInterface(){ //why does it work? in this case
//where is the outer object?
int a, b;
#Override
public void print() {
}};
}
if the anonymous class is an inner class why it doesn't need an outer object?
You're basically asking two questions here.
Why does a non-static nested class require an enclosing instance?
Because it's assumed to have access to all non-static members of the outer class. Even if you don't actually use any of those, the compiler can't infer that the non-static nested class can be used safely in static context. You have to explicitly mark it static to allow this.
Where and what is the enclosing object of the anonymous class created in this snippet?
public class OuterClass {
private int nonStaticMember;
private static int staticMember;
public static void main(String[] args) {
MyInterface myAnObj = new MyInterface(){ //why it works ?? in this case
//where is the outer object?
#Override
public void print() {
//nonStaticMember is not visible in this scope
//staticMember is visible in this scope
}};
}
}
In this case, your anonymous class has no enclosing instance. It's created in a static context, in the main method. You can instantiate MyInterface because all interfaces are implicitly static, even if defined inside another class. That's why the interface is visible at all. All of the non-static members of OuterClass, on the other hand, are not available in this scope so you're guaranteed not to use any of them. This is why there is no need to have a reference to an enclosing object. In fact, this is included in the language's specification.
Take a look at this Java Language Standard excerpt
Let C be the class being instantiated, and let i be the instance being
created. If C is an inner class then i may have an immediately
enclosing instance. The immediately enclosing instance of i (§8.1.3)
is determined as follows.
If C is an anonymous class, then:
If the class instance creation expression occurs in a static context
(§8.1.3), then i has no immediately enclosing instance.
Otherwise, the immediately enclosing instance of i is this.
(...)
I only quoted the most relevant part for this use case. Feel free to dive a little deeper into it.
public class Anonymous2 {
private int numb = 100;
public static void main(String[] args) {
MyInterface myAnObj = new MyInterface(){ //why it works ?? in this case
//where is the outer object?
int a, b;
#Override
public void print() {
System.out.println(numb);
}};
}
}
interface MyInterface {
public void print();
}
Compiling:
C:\JavaTools>javac Anonymous2.java
Anonymous2.java:11: error: non-static variable numb cannot be referenced from a static context
System.out.println(numb);
^
1 error
As can be seen, the code that the OP claimed would compile doesn't. So this entire question is irrelevant.