Creating objects with multiple classes - java

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 {
}

Related

Is there different between this two ways of creating an object

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
}

Transferring an object over multiple class instances

I am thinking of an optimum design pattern which I can use to transfer objects to the methods in different classes other than passing them as arguments.
class A{
}
class B{
public A a;
public B()
{
a = new A();
}
}
class C
{
public void c()
{
//need to access "a" of class B other than passing "a" as argument;
}
}
Here, a in class A attribute needs to be accessed in many other class methods. Is there an optimum design pattern or any possible way other than passing this object (a) as arguments?
It's hard to say how your program is really structured but two options come to mind:
Pass an instance of B to C's constructor.
class A {};
class B {
public A a;
public B() {
a = new A();
}
};
class C {
public B b;
public C( B b ) {
this.b = b;
}
public void someMethod() {
System.out.println( b.a );
}
};
If only one instance of class A ever exists (ie a Singleton). That means that class B holds an instance of class A, not each instance of class B holds an instance of class A.
class A {};
class B {
public static final A a = new A();
};
class C {
public void someMethod() {
System.out.println( B.a );
}
};

Why can I not access an inner class in Java? [duplicate]

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();
}

(Java) Difference ways of instantiating subclasses

I have these two classes:
public class A {}
public class B extends A {}
In the main class, I would declare:
A a = new B();
B b = new B();
What is the difference between a and b ? Is this what we called a subtyping in java?
The difference between a and b is that with a you can only use the public API that the A class provides even though its really a B type object, but with b, you can use both the A's public API as well as B's public API.
For example:
Suppose A and B are defined as follows:
// A.java
public class A
{
public void x()
{
z("X");
}
public void y()
{
z("Y");
}
protected void z(String message)
{
System.out.println(message);
}
}
// B.java
public class B extends A
{
public void a()
{
z("A");
}
public void b()
{
z("B");
}
}
And here's a demo:
// Demo.java
public class Demo
{
public static void main(String[] args)
{
A a = new B();
B b = new B();
// Can only call A's public methods
a.x();
a.y();
// a.a(); Can't use
// a.b(); Can't use
// Can call all public methods
b.a();
b.b();
b.x();
b.y();
}
}
Yes, there is difference between them. Accssibility of methods are different depends on what kind of reference you use.
A a = new B();
B b = new B();
a is a reference of Class A and b is a reference of class B. super class always can be used to point subclass object.
reference a able to access only super class method and properties
reference b able to access super class and it's own method and properties
one important thing is, ability of accessibility of function and properties will decided at runtime.
In below two cases
A a = new B();
a is an instantiation of B and of type A.
whereas in
B a = new B();
a is an instantiation of B and of type B.
The important thing to note here is that (in the first case) if you call a.someMethod(), the method of B will be called, not the method of A (this is called dynamic binding, as opposed to static binding).
This is basic inheritance. In the B b = ... case you can access all methods and variables provided from A and B but in the A case you can only use the methods and variables in the A case.
B gets typecasted into A when it is created, but that information is not required by the compiler.
public class A {
int x;
}
public class B extends A {
int y;
}
A a = new B();
B b = new B();
int c = a.x; //works
int c = a.y; //doesn't work
int c = b.y; //works
int c = b.x; //works
Remember, that you can always cast an object "downwards" in the inheritance chain. But you should not cast a object upwards because the variables for the subclass might be used even though they dont exist, for exmaple B b = new A(); So b.y is dangerous since the B object doesn't have an y variable defined.

Accessing abstract nested class in java

Suppose I have a class say A which is an ordinary class.
Now A nests a class B , which in turn nests C.
Class B is an abstract class while A,C are not so.
Now, No object can be created for B as it is an abstract class.
But Unless i create an object for class B , i wont be able to create an object for class C.
since ,
outerclass out=new outerclass();
outerclass.innerlcass in=outerclass.new innerclass();
Since B has no object , the outerclass object "out" is not available in this case.
So how do I create an object for C?
One way:
public class A {
abstract class B {
abstract String get();
class C {
void run() {
System.out.println(get());
}
}
}
public static void main(String[] args) {
new A().new B(){ String get() { return "hi"; } }.new C().run();
}
}
Or alternately, with the same A, B, and C as above:
class ConcreteB extends B {
String get() {
return "I'm not anonymous!";
}
}
public static void main(String[] args) {
new A().new ConcreteB().new C().run();
}

Categories

Resources