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.
Related
Being new to Java but an old hand on older procedural languages and structured programming, I have a question on how to accomplish something in Java
I have three classes, let's say they're called CLASSA, CLASSB, and TESTCLASSA. CLASSA has a class definition with instance variables, and a constructor for some data. TESTCLASSA creates an instance of CLASSA and passes data to CLASSA by creating an instance of the object for CLASSA. SImiliarly CLASSB has another class definition with instance variables, and a constructor for some data. TESTCLASSA creates an instance of CLASSB and passes data to CLASSB by creating an instance of the object for CLASSB. I am trying to access CLASSB's data from CLASSA. Can someone suggest how I might go about doing this. Many thanks in advance for any assistance/suggestions you can provide.
Wayne Hann
Either declare the variable as public:
public class classA {
public Integer data;
}
or create a public getter (preferred), such as:
public class classA{
private Integer data;
public Integer getData() {
return data;
}
}
If you want to access properties of class B from class A then it's either possible that you create a object of B in the method from where you want to getData like
private String nameofA;
public String getNameofA() {
return nameofA;
}
public void setNameofA(String nameofA) {
this.nameofA = nameofA;
}
public String getClassBData(){
B b = new B();
return b.getNameofB();
}
else you create a class level instance or dependency of B type.
public class TestClassA{
public static void main(String[] args) {
B b = new B();
b.setNameofB("class B Name");
A a = new A("class A Name",b);
}
}
class A{
private String nameofA;
private B b = new B(); //either this
public String getNameofA() {
return nameofA;
}
public A(String nameofA, B b) {//or constructor
super();
this.nameofA = nameofA;
this.b = b;
}
public void setNameofA(String nameofA) {
this.nameofA = nameofA;
}
public String getClassBData(){
B b = new B(); // or creating local instance
//but here a new instance will be created
return b.getNameofB();
}
}
Then only you will be able to access the data of instance of B. Anyway you if your method in B is not private or protected(assuming A doesn't extend B), you can access the method by creating or passing a instance of B to the method of "A" from where you want to access.
I want to design a class which should return a singleton of some third party object.
For e.g., I want to create a singleton of 3rd party B class object. Below is the design I have made.
public class A{
private static A A = null;
private static B B = null;
private A() {
B = code to instantiate B Object;
}
public static synchronized A getAInstance() {
if(A ==null){
synchronized(A.class){
if(A == null){
A = new A();
}
}
}
return A;
}
public B getB(){
return B;
}
}
Can you please help me is this a proper singleton
If I understand your question correctly, you want to have a single instance of a third party class. First of all it is a good practice to access third party obj via wrapper class obj,(clean code handbook of agile software craftsmanship chapter8), in your case class b is wrapped by class a.
In order to make a single instance of class b you can just make it an instance variable of class a and then make the class a singleton, code bellow
Public class A{
private static A A = null;
private B B = null;
private A() {
B = code to instantiate B Object;
}
public static synchronized A getAInstance() {
if(A ==null){
synchronized(A.class){
if(A == null){
A = new A();
}
}
}
return A;
}
public B getB(){
return B;
}
}
You can simply have this structure. No explicit synchrnoization required, just leave it to JVM.
public class A {
private static class BInstanceHolder {
B BInstance = new B();
}
private A(){}
public static B getB(){
return BInstanceHolder.BInstance;
}
}
If you only want to have one copy of B, just do it that way!
You dont even need a Singleton of Class A.
So you could try:
public final class A{
private A(){}
private static B instance;
static{
instance = code to instantiate B Object
}
public static synchronized B getInstance() {
return B;
}
}
The static Block will create a instance of B when the Class is first mentioned and will instantiate the instance. The constructor will prevent the A from being made, but you can still access the only instance of B.
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 );
}
};
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.
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.