简体   繁体   中英

How Do You Pass a Variable from One Class to Another in Java?

I have two java classes named x.java and y.java .

In x.java I have a protected method:

protected ResultSet populateSpeciesList(ResultSet results)

and inside of this protected method, I have an if statement:

if (fsTitle != null)
{
    sp.setFireStudyTitle("Available");
    String sppAcronym = results.getString("ACRONYM");

    FireStudyQueryBuilder qb = new FireStudyQueryBuilder();

    this.magicString = qb.buildMagicString();
}

now in y.java I have a method that goes something like this:

String buildMagicString()
{                 
    String sppAcronym = getAcronym();

    String newQueryString = a bunch of SQL;

    return newQueryString;
}

The problem is, I can't use sppAcronym in the other class because it's not recognized.

So my question is, how do I pass the variable from class to another?

Thanks.

Can you not rewrite your method buildMagicString() like the following?

String buildMagicString(String sppAcronym) //This allows the caller to pass a argument to this method.
{                 

    System.out.println(sppAcronym); //This statement will compile because sppAcronym is a valid member of this method.

    String newQueryString = a bunch of SQL;

    return newQueryString;
}

Then you simply call the method as follows:

if (fsTitle != null)
{
    sp.setFireStudyTitle("Available");
    String sppAcronym = results.getString("ACRONYM");

    FireStudyQueryBuilder qb = new FireStudyQueryBuilder();

    this.magicString = qb.buildMagicString(sppAcronym); //Passes the sppAcronym object to the buildMagicString() method.
}

Pass your variable in the method as follows

this.magicString =  qb.buildMagicString(sppAcronym);

and the method in your another calss is

String buildMagaicString(String sppAcronym){

//do whatever you want with sppAcronym
   return yourDesiredResult;
}

Here's a hint:

String buildMagicString (String sppAcronym) {
    ...
}

Go from there, it's very straightforward -- it's exactly the same idea as the way results is passed to populateSpeciesList() .

You can't use the sppAcronym variable in the other class simply because it's a local variable . Actually you can't even use that elsewhere in the same class too, it's scope only resides in the specified method. If you really need to use that variable value in other class, declare it as private field and then access via getters and setters.

You have tons of ways. The correct one for you depends on the structure of object model you use.

However, there are two basic concept you have to understand:

1. What is the "intersection point".

In order for A 's instance (call it a1 ) to be able to communicate with B 's instance ( b1 ), a1 must have a way to "put his hands" on a reference to b1 . A few example ways to do it are:

  • Each A get's a reference to B 's instance as an argument to it's constructor, or another 'set' method, and then stores it as a field made for that purpose.
  • a1 and b1 share a common instance of class C . c can be the 'parent' of both a1 and b1 (ie contain both of them), or some 'manager' component that manage a certain process in your program.
  • The static \\ singleton way. B class stores a static instance of itself, which allows reference to it from everywhere in the program.

2. What is the the desired communication interface

The interface one class exposes to others should be well designed in order to achieve many important concepts, eg: readability of code, security and 'hiding', reliability etc. This also depends on if both A and B are stored in the same package or are internal classes of each other or are even inherited from each other.

A few standard ways to communicate are:

  • Direct writing access: a field in B is public ly exposed and allows direct writing into it. this is mostly a bad behavior (a rule of thumb is that all fields should be at least protected if not private ).
  • set methods: B has a set method that receives the calculated data, process it and stores it in it's fields (or pass it on).
  • Listening - A stores the calculated data in itself, and lets all of it's registered "listeners" know that a new data is available by calling an appropriate method in them. The listeners have read access to the relevant fields, or the data is passed as an argument, and then the listener ( b1 ) decides what to do with the data.

well the most convenient way to do this is by making the sppAcronym as private variable and providing public setters and getters for the same , this way u can create an object of the class y.java like

y obj=new y();

and use the setter and getter in your x.java an example can be :-

in y.java

private String sppAcronym;
public void setsppAcronym(String sppAcronym)
{
this.sppAcronym=sppAcronym;
}
public String getsppAcronym()
{
return sppAcronym;
}

in x.java

y obj=new y();

obj.setsppAcronym(getAcronym);
String sppAcronym=obj.getsppAcronym();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM