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 );
}
};
Related
I have a class A and within class A I have function func1 and func2.
In the same file I have class B and in that class B I have function func3.
In the main class, an object obj1 of class A is declared.
With this object, func1 of class A is called.
Within func1 of class A, an object obj2 of class B is created.
With this object, func3 of class B is called.
Now within func3 of class B, i want call func2 of class A with the object obj1. For this I want to refer to that object from within func3 of class B. Is it possible? If yes, how?
I tried using this.this.func2 which wouldn't work.
For now I am passing the object obj1 as an argument and it works fine. But I want to do the same without passing it because I want to use an array of objects and every time the object should differ
class A {
int attr1, attr2;
public void func1() {
int attr1 = 3;
int attr2 = 6;
B obj2 = new B();
obj2.func3();
}
public void func2() {
this.attr1 = 5;
this.attr2 = 10;
}
}
class B {
int atr1, atr2;
public void func3() {
atr1 = 4;
atr2 = 8;
// here I want to access the object obj1 to call the function func2()
}
}
public class Main {
public static void main(String args[]) {
A obj1 = new A();
A.func1();
}
}
Is it possible?
Yes.
If yes, how?
Option 1: Pass as parameter
Pass obj1 as parameter to func3.
Or more precisely, since func3 is called from a method of obj1, pass this as the parameter value:
class A {
public void func1()
{
B obj2 = new B();
obj2.func3(this);
}
}
class B {
public void func3(A a)
{
a.func2();
}
}
Option 2: Pass to constructor
Pass the A reference to the B constructor, and have B remember it in a field.
class A {
public void func1()
{
B obj2 = new B(this);
obj2.func3();
}
}
class B {
A a;
public B(A a) {
this.a = a;
}
public void func3()
{
this.a.func2();
}
}
Option 3: Inner class
Make class B an inner class of A. Essentially the same as option 2, but the compiler handles the reference to A for you.
class A {
public void func1()
{
B obj2 = new B();
obj2.func3();
}
class B {
public void func3()
{
A.this.func2();
}
}
}
I'm not novice in java, but I've got an example that made me confused.
Here it is:
class A {
public A() { System.out.print("O"); }
}
class B {
{ System.out.print("A"); }
public B() { System.out.print("D"); }
}
class C {
static { System.out.print("3"); }
public C() { System.out.print("P"); }
}
public class D extends C {
private A objA = new A();
private static B objB = new B();
public D() { System.out.print("T"); }
public static void main(String[] args){
new D();
}
}
So what is the result in system.out?
We know that static members are the first, so "3" will print first cause it is in the superclass and private static B objB = new B(); will initialize after it (instance initializer and then constructor).
We get 3AD in console.
Then the main method runs and create a new instance of class D, its ok.
But since this step the order is strange:
1 Constructor of the superclass public C() { System.out.print("P"); }
3ADP in console.
2 Then field of D.class private A objA = new A();
3ADPO in console.
3 And constructor of D.class is the last, so:
3ADPOT in console.
So the question is: why does superclass constructor run before field of subclass? I thought that constructors have lowest priority. Can anyone share a link on docs plz?
Simply if we think then its easy to understand that first the subclass inherits from the super class and then only can it override the behaviour or acess the properties defined in the super class.
Whenever we create an object of a class A , first it is checked if the class is loaded, if not then it is loaded which invokes its static initilizer if it is present, then all the static fields are initilized (with default value or the values defined). After this super constructor is called, all the properties as set by the super class are set. Then all the instance fields are initilized and then finally constructor code of A is executed.
Below is the execution ordering.
public class A{
static{
// 1
}
int x = 30; // 3
public A(){
//4
}
}
public class B extends A{
static{
//2
}
private int s = 60; //5
public B(){
//6
}
}
public class Test {
public static void main(String[] args){
new B();
}
}
Let's say I have 3 classes; A , B and C. Class A and B are in one package. C is in another package. B is having a public function that returns a boolean value.
Object of B is created in Class A and he can call the functions of Class B. But the problem is that in Class C I want to have the reference of Class B object created by Class A, I don't want to create one more Class B object in Class C.How can I get that.
Example: Class B and Class A are in same package
B.java
class B
{
public boolean fun()
{
returns boolValue;
}
}
A.java
class A
{
B b = new B(); //Creates object of Class B and can access function.
}
Class C is in another package and different project also.
C.java
class C
{
//How to get the reference of Class B object created in Class A?
}
Once I get the reference to that I am going to call the functions of Class B to get the values. I tried writing some get() which returns the object of class B but to call that function I should have the object. But I don't know how to do that. This might be easy but I am new to java and I don't know how to do that. Please do help me to solve this problem.
**UPDATE : I can't create Class A object in Class C :( **
Make class A public
publi class A {
private static B b = new B(); //Creates object of Class B and can access function.
public static B getB() {
return b;
}
}
class C
{
//I want to get the reference of Class B object created in Class A....But How??? :(
B b = A.getB(); //if it works ????
}
Classes A and B need to be public and class A needs to provide a public accessor to the required field.
public class B {
}
public class A {
public B getB() {
return this.b;
}
}
Note that in your current code structure you will not be able to automatically instantiate B from your A object outside of a method.
You can create an A object in the class description but to initialize B you'll need to use the class constructor.
public class A {
private B b = new B();
public B getB() {
return b;
}
}
public class C {
private A a = new A();
private B b;
public C() {
b = a.getB();
}
}
or, if you don't want to create A, you may make it static.
public static class A {
private static B b = new B();
public static B getB() {
return b;
}
}
public class C {
private B b;
public C() {
b = A.getB();
}
}
You have to get access in C to A class and then share B reference by field.
So you may do something like this:
class B { //your function... }
class A {
B b = new B();
public B getB() { return b }
}
class C {
A a = new A();
public B getB () { return a.getB() }
}
}
Add a constructor to class C with object b as parameter
public class C{
private B bObj;
public C(B b){
bObj=b;
}
}
call this constructor from within your A class, which will create a reference to the created B object and you'll have this ref in bObj.
I have class In java: A. And class B which extends class A.
Class A hold instance of class B.
I notice that when I call the constructor of class B (when I init this parameter in class A), It does super(), create a new instance of A and init all it fields.
How I can tell class B that the concrete instance of class A (which init it field) - it his parent class?
Your question is really hard to understand, but I guess the problem is this this (your approach):
public class A {
public B b = new B();
}
public class B extends A {
}
So, when you run new A() you get a StackOverflowError.
In my practical experience, I never needed a design like that, and I'd strongly recommend to re-think your approach. However, if it is really needed, you could use a dedicated init() method, e.g.:
public class A {
public B b;
public void init() {
b = new B();
}
}
A a = new A();
a.init();
If you needed A within B you could just do it with a custom constructor for B:
class B extends A {
A a;
public B() {
super();
this.a = this;
}
}
This case is harder though so you need:
class A {
B b;
}
class B extends A {
public B() {
super();
b = this;
}
}
Note that you should not pass the B into the call to super() as B will not be initialized, you should do it as the last step in the constructor of B.
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();
}