Can JavaBean retrieve another JavaBean Property?

foongkim2
foongkim2 used Ask the Experts™
on
Hi, the question title is clear is it? I have a senario which I think I am out of ideas.

I would like to ask is it possible for JavaBean1 retrieve the JavaBean2's property??

JavaBean1.java
==============

private String var1 = "";
...
...
public void setVar1(v1) {
       var1=v1;
}
public String getVar1() {
   return var1;
}
...
...


JavaBean2.java
==============
....
import abc.javabean1
...
...
String pk = javabean1.getvar1();
..
..
..

Can it be done??
I think it's shouldn't be any problem. The only concern is the location of the bean, the import location. Let's say my JavaBean1 is store in abc/pk/JaveBean1.java and JavaBean2.java is in the same directory means abc/pk/JavaBean2.java, how would be my import statement look like??





Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Commented:
Yes it can be done, but not by importing (though you need the import to use the class).
Importing is used to specify the classes used, not instances of classes.

You instead need to pass a reference of your bean to your other bean:

import abc.JavaBean1

public class JavaBean2
{

   private JavaBean1 javabean1;

   public void setJavaBean1(JavaBean1 bean)
   {
      javabean1 = bean;
   }

...

Author

Commented:
Object, confused.
Let's discuss throguht the code I posted just now.

javabean1.java
==============
....
package com.wrox; //location of my JavaBean1
...
...
public class javabean1 {

 private String var1="";
...
...
public void setVar1( Sting v1) {
     var1=v1;
}

public String getVar1() {
     return var1;
}
..
..
}

javabean2.java
==============
...
...
package com.wrox; // location of my javabean2
import com.wrox.javabean1; //Do i need this???
...
private javabean1 jb1;
...

public void setJb1 ( String j1 ) {
     jb1 = j1;
}
public String getJb1 (
     return jb1;
}
...
...


Did I code it right?? And the import com.wrox.javabean1 should be there?? ( I think it should )

Mick BarryJava Developer
Top Expert 2010

Commented:
The import does not import an object instances.
It just tells the compiler what class(s) you are using.

Your code is fine, but you need to somewhere set the value of jb1.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Author

Commented:
OK man.... I think I am improving alot....

Do u agree? Since you are the one who share with me most. ha...:-)
Mick BarryJava Developer
Top Expert 2010

Commented:
Yes you are improving :)

Author

Commented:
Objects, one extra things wish to ask you.

In the JavaBean declaration part,

<jsp:useBean id="abc" class="com.wrox.javabean1: scope="session" />


regarding the "scope" calue, according from the book, that can be set as page, request, session and application. For my senario, which is consists of 4 jsp pages, and 3 javabean, I would like to keep track of the previous data enter by the user. That's why I use this "scope" functiona. Yet, I found some problems.

1. is it when I implement this scope, will the server request more resources? ( i think it will because the JSP container or server need more procresssing power or memory to store the value of the jsp property).

2. If I want to use this scope, which one is the best to use? I have tried the session but I found the session is not tha stable. Because when I put my page idle for some time,it'll gone. ( I think because of the session time out problem).

3. Finally, do u encourage prople to use application, which mean the JSP property can be access all over the JSP application system in all the 4 JSP page and 2 JavaBea in my case.
Java Developer
Top Expert 2010
Commented:
1. no

2. depends on your requirements. if you just need the bean to handle a user session (eg. login details) then use session.

3. See 2. It's not a case of which is best, it is which suits your beans needs.


Author

Commented:
Thanks Object, Thanks for your help.

I am still learning from you. Much much more to learn from you. Ha..

Thanks.
Mick BarryJava Developer
Top Expert 2010

Commented:
:-)

Author

Commented:
Object.... can not again....

"Can not resolve symbol"

debit1bean.java
===============
package com.wrox.cars;

public class debit1bean {
...
private String billcode="";
...
}

stockamount.java
================

package com.wrox.cars;
import com.wrox.cars.debit1bean;

public class stockamount {
   private debit1bean d1;
...
...

Then the error pointing to stockamount.java in debit1bean, saying that cannot resolve symbol...

I am very sure that the debit1bean.java is exist in the directory... com.wrox.cars


 


Mick BarryJava Developer
Top Expert 2010

Commented:
Check that debit1bean.class exists in the directory... com.wrox.cars

Author

Commented:
It's there .......

Do u agree that anything wrong with the declaration of the coding inside the debit1bean.java???

debit1bean.java
===============
package com.wrox.cars;

public class debit1bean {
..
...
}

Author

Commented:
It's there .......

Do u agree that anything wrong with the declaration of the coding inside the debit1bean.java???

debit1bean.java
===============
package com.wrox.cars;

public class debit1bean {
..
...
}

Mick BarryJava Developer
Top Expert 2010

Commented:
no that looks fine.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial