I'm currently working on a uni project where I've made a simple class called "User" which holds things like name, address etc from a database in Java. What I wanted to know was if it was possible to either pass all of the object information into another class
// Getting person's info from DB
// Personal details
String ID = boolPhone.getString("ID");
String fName = boolPhone.getString("FIRSTNAME");
String lName = boolPhone.getString("LASTNAME");
String Phone = boolPhone.getString("PHONENUMBER");
String Email = boolPhone.getString("EMAIL");
// Address lookup via ID
query = "SELECT * FROM ADDRESSES WHERE ADDRESSID = ?";
userLookup = connection.prepareStatement(query);
userLookup.setString(1, ID);
ResultSet addressDetails = userLookup.executeQuery();
if(addressDetails.next() == true){
// Address details
String houseNum = addressDetails.getString("HOUSENUMBER");
String Street = addressDetails.getString("STREETNAME");
String City = addressDetails.getString("CITY");
String Flatblock = addressDetails.getString("FLATBLOCK");
String Postcode = addressDetails.getString("POSTCODE");
// Create class from database
LoggedUser userDetails = new LoggedUser(ID, fName, lName, Phone,
Email, houseNum, Street, City,
Flatblock, Postcode);
For extra context on why I'd need to pass access this specific object's attributes is because it essentially uses the phone number entered as the username. This tells the program which row holds all the user data in the database table and allows for access from other parts of the app as well. I'm using FXML so it'd be accessed from different screens which are stored in the same package but have different controllers as this is a large project so separating controllers and just passing which controller is currently running was the easiest way I could think of. Here's the code which swaps the current root/controller
Parent root = FXMLLoader.load(getClass().getResource("/HomeScreen/homeScreen.fxml"));
Scene rootScene = new Scene(root);
Stage rootWindow = (Stage)((Node)event.getSource()).getScene().getWindow();
rootWindow.setScene(rootScene);
rootWindow.show();
}
I fixed the issue. In case anyone is wondering, you need to set class attributes to global with an empty constructor. That way any instance of the class can edit the attributes
public class LoggedUser { // Personal details // Accessible outside of scope public static String fName; public static String lName; public static String Phone; public static String Email;
// Address
// Accessible outside of scope
public static String houseNum;
public static String Street;
public static String City;
public static String Postcode;
public static String Flatblock;
public static String ID;
public LoggedUser(){
// Empty constructor as we just want to
// Access the global values stored here
}
Then you need Setter methods for each static attribute you want to edit