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();
}
Related
This question already has answers here:
What is method hiding in Java? Even the JavaDoc explanation is confusing
(8 answers)
Closed 5 months ago.
My questions are:
Why method hiding is also an example of compile time polymorphism while it is not overridden?
Related code:
class A{
public static void print(){
System.out.println("class A");
}
}
public class B extends A{
public static void print(){
System.out.println("class B");
}
public static void main(String[] args){
A a = new B();
a.print(); //class A
B b = new B();
b.print(); //class B
}
}
If Method hiding is also an example of compile time polymorphism then why variable hiding not an example of compile time polymorphism while variable hiding like method hiding are not overridden and polymorphic?
Sorry my english is pretty bad.
https://byjus.com/gate/difference-between-compile-time-and-run-time-polymorphism-in-java
Q: What Is Compile-time Polymorphism?
A: Compile-time polymorphism is obtained through method overloading.
The term method overloading allows us to have more than one method
with the same name. Since this process is executed during compile
time, that's why it is known as Compile-Time Polymorphism.
Your code won't compile as-is. Here's an alternate version:
B.java:
class A{
public static void print(){
System.out.println("class A");
}
}
public class B extends A{
public static void print(){
System.out.println("class B");
}
public static void main(String[] args){
A a = new A();
a.print();
B b = new B();
b.print();
A c = new B();
c.print();
//System.out.println(a.a);
}
}
Output:
class A
class B
class A
You'll notice:
A.print() => "class A" // Expected
B.print() => "class B" // Also expected
class B "is a" A // We can instantiate "B", but use it as "A"
C.print() => "class A" // When we do so ... it BEHAVES as "A"
This example illustrates compile time behavior
Now let's try a different example, without "static":
class A2{
public void print(){
System.out.println("class A2");
}
}
public class B2 extends A2{
public void print(){
System.out.println("class B2");
}
public static void main(String[] args){
A2 a = new A2();
a.print();
B2 b = new B2();
b.print();
A2 c = new B2();
c.print();
//System.out.println(a.a);
}
}
Output:
class A2
class B2
class B2
This is an example of runtime polymorphism.
I just have learned java. I'm fiding the different between this both ways of creating an object
public class A {
}
public class B extends A {
}
public static void main(String[] args){
A object = new B();
B object = new B();
}
Lets understand it with the example below.
In class A we added a getMethodofA(). So creating reference variable as A or B does not matter. As A is super class getMethodofA() will be available for both the objects of Type A or Type B
In class B we added a getMethodofB(). So creating reference variable as A or B matters. If you create object with reference variable as A, then only getMethodofA() will be available. While If you create object with reference variable B both the methods will be visible getMethodofA() and getMethodofB()
public class A {
public void getMethodofA(){
System.out.println("I am method A")
}
}
public class B extends A {
public void getMethodofB(){
System.out.println("I am method B")
}
}
public static void main(String[] args){
A objectA = new B();
objectA.getMethodofA();//No error
objectA.getMethodofB();//Compile time error
B objectB = new B();
objectB.getMethodofA();//No error
objectB.getMethodofB();//No error
}
I have a program with multiple classes, and when I try to make an instance of one of these objects in main, I get an error. How do I properly create a class in main with multiple classes?
public class A {
class B {
}
class C {
}
public static void main(String[] args) {
B b = new B();
C c = new C();
}
Error: No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A
This is because B and C are inner classes. Unless you understand inner classes, this is probably not what you want.
Move them outside A:
public class A {
public static void main(String[] args) {
B b = new B();
C b = new C();
}
}
class B {
}
class C {
}
This question already has answers here:
Can java call parent overridden method in other objects but not subtype?
(4 answers)
Closed 8 years ago.
Is there a way to call test() in class a from class b object created in class c ?
class a {
void test(){
System.out.println("in a");
}
}
class b extends a {
void test(){
System.out.println("in b");
}
}
public class c{
public static void main(String[] args) {
b refb = new b();
refb.test();
}
}
You can do that only within the test() method of class b like following.
class b extends a {
void test(){
super.test();
System.out.println("in b");
}
}
In Java, all non static private methods are virtual by default. So, there's no way to invoke a#test from b instance unless you modify b#test. The only way to do it (by your current design) is using an instance of a:
public class c{
public static void main(String[] args) {
b refb = new b();
// code to call test() in class a
//this is the only way you have in Java
a refA = new a();
a.test();
}
}
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